microsoft / microsoft/TypeScript
Reflect Metadata not supported for TC39 decorators
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
decorator, experimentalDecorators, emitDecoratorMetadata, TC39, reflect-metadata, Reflect.metadata(k, v)
🕗 Version & Regression Information
This breaks when setting disabling experimental decorators
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
⏯ Playground Link
https://github.com/paulsmithkc/typescript-decorators
💻 Code
tsconfig.json
{
"compilerOptions": {
"target": "ES2021",
"module": "commonjs",
"outDir": "dist",
"declaration": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
src/index.ts
import 'reflect-metadata';
function setting(defaultValue: string): any {
function getType(target: unknown, property: string | symbol) {
return Reflect.getMetadata('design:type', target, property);
}
function settingExperimental(target: unknown, property: string | symbol): void {
console.log('settingExperimental', { defaultValue, target, property, type: getType(target, property) });
target[property] = process.env[String(property)] || defaultValue;
return;
}
function settingTC39(_target: unknown, context: ClassFieldDecoratorContext): () => string {
return function (): string {
console.log('settingTC39', { defaultValue, target: this, context, type: getType(this, context.name) });
return process.env[String(context.name)] || defaultValue;
};
}
return function (target: unknown, context: string | symbol | ClassFieldDecoratorContext) {
if (typeof context !== 'object') {
return settingExperimental(target, context);
} else {
return settingTC39(target, context);
}
};
}
class Config {
@setting('default_1') SETTING_ONE: string;
}
const configInstance = new Config();
run with:
tsc --project tsconfig.json && node dist/index.js
🙁 Actual behavior
Reflect.getMetadata('design:type', target, property) returns undefined.
🙂 Expected behavior
Reflect.getMetadata('design:type', target, property) returns the type of the class field, when using Standard TC39 decorators.
Additional information about the issue
-
When transpiling with:
"experimentalDecorators": true, "emitDecoratorMetadata": true,The decorator
Reflect.metadata("design:type", type)is automatically applied to each class field. -
When transpiling with:
"experimentalDecorators": false, "emitDecoratorMetadata": false,The decorator
Reflect.metadata("design:type", type)is not applied. -
When transpiling with:
"experimentalDecorators": false, "emitDecoratorMetadata": true,Typescript produces the following error
Option 'emitDecoratorMetadata' cannot be specified without specifying option 'experimentalDecorators'.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führe tsc --project tsconfig.json && node dist/index.js aus dem verknüpften Playground aus und beginne dann mit src/index.ts und seinen Decorator-Aufrufen zusammen mit tsconfig.json. Als abgeschlossen gilt die Aufgabe, wenn der standardmäßige TC39-Decorator-Pfad den Typ des Klassenfelds über Reflect.getMetadata('design:type', ...) statt undefined zurückgibt, ohne die gemeldeten Compiler-Optionseinschränkungen zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100