-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Compile-time meta-data #54672
Comments
This is decorator syntax, and TS already has I'm also confused because you say
What do you want the compiler to do with this metadata, exactly, if it doesn't affect the output? This feature request seems underspecified... |
@fatcerberus I didn't exactly want to mean it affects the output. It can affect the output depending on who uses the TypeScript Compiler API and what they are going to do with the symbols emitted by the compiler. The decorator I've demonstrated shouldn't work as an ECMAScript decorator exactly. And it's only treated as a meta-data decorator depending on the |
Added:
|
Thanks for clarifying. Hmm, I don't know that it's feasible to use the decorator syntax for this though. Someone could very well have defined a real ES decorator called |
@fatcerberus I guess the |
Unfortunately this proposal violates:
Other tools are free to consume TypeScript syntax and perform whatever transformations they need to though. This can even be done by using the TypeScript APIs. Otherwise, https://github.com/tc39/proposal-decorator-metadata is on our plan for TypeScript 5.2. |
@DanielRosenwasser But, yes, it's fully erasable. The meta-data I've been demonstrating are constant (compile-time) values associated to definitions purely for developers using the compiler API. This meta-data cannot be accessed from the script itself. I don't think it's out of scope. |
This proposal seems unrelated as it's about adding meta-data that can be accessed at runtime. |
Then there's still this point:
If the decorator is removed, then the runtime behavior of the JavaScript code is not preserved anymore. |
Then I guess a new syntax that does not use the decorator syntax could be used, that is specific to TypeScript? Anyway, the option |
Another thing to consider is that if the decorator is overriden by the script, then this @Metadata() // 'Metadata' is undefined; remove it
var x = 0 And: var Metadata = () => {}
@Metadata() // 'Metadata' exists; don't remove it
var x = 0 This is of course if |
In that case, maybe #2900 is relevant. |
This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
π Search Terms
Meta-data, definition meta-data
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Enable optionally adding compile-time meta-data to a specific definition, which is only interpreted depending on a compiler option. The meta-data does not affect code generation and is erased completely in runtime, at least in TypeScript's Compiler. However, a transform that a TypeScript Compiler API user writes can emit customized code based in that meta-data.
This could be useful for TypeScript Compiler API users that need to attach data to definitions, such as the name of a natively exported function (FFI) and then transform code based on that meta-data.
As for the name visibility, I'd recommend only declaring this
Metadata
decorator name in the decorator scope; e.g. a decorator consisting of aMetadata
call, and this would only be treated as meta-data if theallowMetadata
compiler option is on. The current script can override theMetadata
name.This proposal isn't the same as https://github.com/tc39/proposal-decorator-metadata: they're not accessible from the runtime.
π Motivating Example
If a compiler option
allowMetadata
is true,Metadata
decorators are allowed in any definition, includingclass
, class block definitions,interface
and more. TheMetadata
name should be similiar to the type annotationsRecord
andNonNullable
.π» Use Cases
FFI for compiler API users. I think an existing workaround is to use very specific comments such as
//!metadata=com.adobe.air.C
and check if they belong to a definition, but that can be problematic if the definition already has doc comments applied to it, e.g.:The text was updated successfully, but these errors were encountered: