Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect Metadata not supported for TC39 decorators #55788

Open
paulsmithkc opened this issue Sep 19, 2023 · 7 comments
Open

Reflect Metadata not supported for TC39 decorators #55788

paulsmithkc opened this issue Sep 19, 2023 · 7 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@paulsmithkc
Copy link

paulsmithkc commented Sep 19, 2023

🔎 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'.
    
@fatcerberus
Copy link

AFAIU this is not implemented yet. See #53461.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Sep 20, 2023
@LabEG
Copy link

LabEG commented Dec 20, 2023

Same problem. Lots of libraries use metadata about types, such as Angular, NestJs, class-validator, my library for di. Generating metadata for new decorators is very necessary.

@Sayan751
Copy link

I am also seeing this issue. Here is the playground link.

I have set the emitDecoratorMetadata to true, but the emitted code does not contain the desgin:type metadata.

@tsangste
Copy link

tsangste commented Jan 27, 2024

I think the title of issue needs to be changed to something like TC39 Decorators does not emit metadata to Symbol.Metadata

Also reviewing the code it looks like it is not implemented here https://github.com/microsoft/TypeScript/blob/v5.3.3/src/compiler/transformers/ts.ts#L1064

@artberri
Copy link

I also thing that emitting type metadata is very necessary, but now that both Decorators and Decorator Metadata have achieved Stage 3 within TC39 and have been implemented in TS >=5.2, the API proposed in the reflect-metadata library is no longer being considered for standardization.

This is why I think that is better to expose design-time type information in the new TC39 decorator metadata when emitDecoratorMetadata: true.

@bcheidemann
Copy link

I also thing that emitting type metadata is very necessary, but now that both Decorators and Decorator Metadata have achieved Stage 3 within TC39 and have been implemented in TS >=5.2, the API proposed in the reflect-metadata library is no longer being considered for standardization.

This is why I think that is better to expose design-time type information in the new TC39 decorator metadata when emitDecoratorMetadata: true.

Inclined to agree @artberri. I've raised a PR to serve as a PoC / proposal for how this could be implemented. A lot of the foundations were already set, so this barely required any changes.

@ianzone
Copy link

ianzone commented May 3, 2024

Mark nestjs/nest#11414

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants