microsoft / microsoft/TypeScript

Reflect Metadata not supported for TC39 decorators

Abierto
#55,788 9 comentarios 19 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔎 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
  1. When transpiling with:

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    

    The decorator Reflect.metadata("design:type", type) is automatically applied to each class field.

  2. When transpiling with:

    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    

    The decorator Reflect.metadata("design:type", type) is not applied.

  3. When transpiling with:

    "experimentalDecorators": false,
    "emitDecoratorMetadata": true,
    

    Typescript produces the following error

    Option 'emitDecoratorMetadata' cannot be specified without specifying option 'experimentalDecorators'.
    

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Ejecuta tsc --project tsconfig.json && node dist/index.js desde el playground enlazado y, después, empieza por src/index.ts y sus llamadas a decorators junto con tsconfig.json. Se considera terminado cuando la ruta estándar de decorators de TC39 devuelve el tipo del campo de clase mediante Reflect.getMetadata('design:type', ...) en lugar de undefined, sin cambiar las restricciones de opciones del compilador indicadas.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.