You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"ReferenceError: Cannot access class before initialization", "experimentalDecorators", "experimentalDecorators before initialization", "emitDecoratorMetadata", "emitDecoratorMetadata initialization", "emitDecoratorMetadata ordering"
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about decorators, ordering and initialization, emitDecoratorMetadata
which references B before it is defined. But if I remove the @Injectable() decorator from class A, the JS output is valid because the __decorate block mentioned above is not generated.
🙂 Expected behavior
I would probably expect 1 of two things:
1/ TS would emit valid JS irrespective of whether the decorator is applied
or
2/ TS would emit an error against constructor(private readonly b: B) {} during compilation (saying that B is not yet defined) - if emitDecoratorMetadata is enabled
but this took a bit of time to realised because it's not initially obvious that it's the Injectable() that's affecting the order you can define the classes/types in - ie. normally you can reference types before initialization just fine
typeO={c: C;// OK}classC{}
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
"ReferenceError: Cannot access class before initialization", "experimentalDecorators", "experimentalDecorators before initialization", "emitDecoratorMetadata", "emitDecoratorMetadata initialization", "emitDecoratorMetadata ordering"
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?noImplicitAny=false&strictNullChecks=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&alwaysStrict=false&noLib=true&removeComments=true&experimentalDecorators=true&emitDecoratorMetadata=true&target=5&jsx=0&module=1&inlineSources=true&isolatedModules=false&ts=5.5.0-dev.20240325#code/JYWwDg9gTgLgBAbzgSQHYCsCmBjGBDAIwBtM4BfOAMyghDgCIABVTAZxnVYHptaQJU9ANwAoEYzRZchEgAoAlCMwAPSLDjYieVqzgBBRCLgaB7KAFdc0WWCjAAbnhikomPABMBRAJ5wCALjgAIXlEMhFw8UkcfGJMBSVVaHhNbV0gw2NeVDNLGGtbBycXN09UHw1As2BUAHNQhHCyIA
💻 Code
🙁 Actual behavior
When running the output file
node out.js
you get this error:Emitted JS is as follows:
In particular this part:
which references
B
before it is defined. But if I remove the@Injectable()
decorator fromclass A
, the JS output is valid because the__decorate
block mentioned above is not generated.🙂 Expected behavior
I would probably expect 1 of two things:
1/ TS would emit valid JS irrespective of whether the decorator is applied
or
2/ TS would emit an error against
constructor(private readonly b: B) {}
during compilation (saying thatB
is not yet defined) - if emitDecoratorMetadata is enabledAdditional information about the issue
Tested against: 5.3, 5.4, v5.5.0-dev.20240325
Other setings: ES2018 output, CommonJS module,
emitDecoratorMetadata: true
,experimentalDecorators: true
The easy workaround here is to re-order the classes so that
B
comes before it's mentioned in A's constructor:but this took a bit of time to realised because it's not initially obvious that it's the
Injectable()
that's affecting the order you can define the classes/types in - ie. normally you can reference types before initialization just fineThe text was updated successfully, but these errors were encountered: