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

"undefined" passed for the "context.metadata" field in a decorator metadata #55453

Closed
mushketyk opened this issue Aug 21, 2023 · 2 comments
Closed
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@mushketyk
Copy link

πŸ”Ž Search Terms

"decorator metadata" in both closed and opened issues.

πŸ•— Version & Regression Information

  • context.metadata is undefined in a decorator function

⏯ Playground Link

No response

πŸ’» Code

Code mostly copies the example from the TypeScript 5.2 release notes.

function setMetadata(_target: any, context: any) {
    console.log(`@setMetadata - ${context.name}`)
    context.metadata[context.name] = true;
}

class SomeClass {
    @setMetadata
    foo = 123;

    @setMetadata
    accessor bar = "hello!";

    @setMetadata
    baz() { }
}

const ourMetadata = SomeClass[Symbol.metadata];

console.log(JSON.stringify(ourMetadata));

πŸ™ Actual behavior

Script fails when trying to set a value on the context.metadata field.

@setMetadata - bar
/Users/ivanmushketyk/Development/courses-creation/pluralsight-typescript-decorators/metadata.js:48
    context.metadata[context.name] = true;
                                   ^

TypeError: Cannot set properties of undefined (setting 'bar')
    at setMetadata (/Users/ivanmushketyk/Development/courses-creation/pluralsight-typescript-decorators/metadata.js:48:36)
    at __esDecorate (/Users/ivanmushketyk/Development/courses-creation/pluralsight-typescript-decorators/metadata.js:12:40)
    at /Users/ivanmushketyk/Development/courses-creation/pluralsight-typescript-decorators/metadata.js:78:13
    at /Users/ivanmushketyk/Development/courses-creation/pluralsight-typescript-decorators/metadata.js:82:11
    at Object.<anonymous> (/Users/ivanmushketyk/Development/courses-creation/pluralsight-typescript-decorators/metadata.js:84:2)

πŸ™‚ Expected behavior

It should not fail, and metadata should be displayed using the console.log statement

Additional information about the issue

Compiler version:

tsc --version
Version 5.2.1-rc
@a-tarasyuk
Copy link
Contributor

Are you utilizing a polyfill to define Symbol.metadata?

Because this feature is still fresh, most runtimes will not support it natively. To use it, you will need a polyfill for Symbol.metadata.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Aug 22, 2023
@mushketyk
Copy link
Author

@a-tarasyuk Good catch! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants