Skip to content

Compile-time meta-dataΒ #54672

Closed as not planned
Closed as not planned
@hydroper

Description

@hydroper

Suggestion

πŸ” Search Terms

Meta-data, definition meta-data

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ 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 a Metadata call, and this would only be treated as meta-data if the allowMetadata compiler option is on. The current script can override the Metadata 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, including class, class block definitions, interface and more. The Metadata name should be similiar to the type annotations Record and NonNullable.

@Metadata({ffi: 'com.adobe.air.C'})
class C {
}

πŸ’» 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.:

//!metadata=com.adobe.air.util.f
/**
 * Does something.
 */
declare function f(): void;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions