tc39 / proposal-decorator-metadata

MIT License
137 stars 9 forks source link

Support to stage 3 #14

Open pabloalmunia opened 1 year ago

pabloalmunia commented 1 year ago

It's important the next TC39 meeting session about Decorator Metadata for Stage 3 (slides, spec). We need the decorator metadata because, without it, we have limited the decorator feature.

Especially important is - at least for some teams - to be able to coordinate decorators with metadata, especially the class member decorators, which in this current pattern as mere metadata annotators, and the associated class decorator, which reads from the metadata and performs the necessary transformations and adaptations.

As a workaround, when we use "method", "getter", "setter" or "accessor" decorators, we can put a symbol in each function to put metadata there, but with "field" we don't have a shelf where to put metadata.

Of course, without this feature, work with metadata and inheritance is very complicated.

Consequently, we need the decorator metadata as soon as possible.

transitive-bullshit commented 3 months ago

Adding some context for fellow decorator explorers 😄

TypeScript added support for decorator metadata in this PR which is described in their 5.2 release notes.

esbuild has also added support for this proposal here.

Can anyone from tc39 provide clarity on a few questions here:

  1. Why is this proposal separate from the core decorators proposal given that they overlap so much and from what I can tell, many of the MVP use cases for decorators require metadata to work? Is it because decorators were already such a complicated feature that it was more practical to split them up?
  2. What is the status of this decorator metadata proposal?

This comment from the TS discussion tracking decorator metadata by @EisenbergEffect would seem to imply that the decorator metadata proposal is stage 3 alongside decorators, but I'm not sure the extent to which that's talking about decorators vs decorator metadata.

btw I'm currently using the following polyfill for decorator metadata:

// https://github.com/microsoft/TypeScript/issues/53461
// symbol-polyfill.ts

declare global {
  interface SymbolConstructor {
    readonly metadata: unique symbol
  }
}

;(Symbol as any).metadata ??= Symbol.for('Symbol.metadata')

const _metadata = Object.create(null)

if (typeof Symbol === 'function' && Symbol.metadata) {
  Object.defineProperty(globalThis, Symbol.metadata, {
    enumerable: true,
    configurable: true,
    writable: true,
    value: _metadata
  })
}

Thanks! 🙏