-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Implement the updated JS decorators proposal #48885
Comments
I think the first step will be implementing decorators. We're not going to try to model the "decorators can change the types of things" portion at first, but I think we can get something good and working. |
@DanielRosenwasser that sounds perfect. But just to be clear, annotating how a decorator change’s a type will be on the roadmap long term? |
Shhh, don't say "long term". 😉 |
@DanielRosenwasser the biggest "type-related" transformation that comes up for me in the current implementation of decorators is communicating that a field decorator initializes the field's value. I think that this would just fall out of Also, a regular field decorator might still want to communicate that the value is initialized (for example, a decorator that implements a default by returning a new initializer). Is it possible to address the issue of communicating that a decorator initializes the value on a different timeframe than arbitrary type transformation, or is it just as hard? |
Simple transform decorators should be easy enough to support right? Like: function wrapInBox<R>(f: () => R): () => { value: R } {
return () => { value: f() };
}
class Foo {
@wrapInBox
method(): number {
return 3;
}
}
const foo = new Foo();
foo.method(); // should be { value: number }, same as regular function wrapping Has very little difference to (type inference wise): class Foo {
method = wrapInBox((): number => {
return 3;
});
} (Ignoring the prototype vs own property distinction here). Isn't it primarily the metadata-based type-mutations that are hard to actually support? (given they need to communicate types across calls) |
Besides this, also picking types from a class by specific decorator would be wonderful... in the longer term. :D |
Do you mean something like this? class Foo {
@alwaysString foo = 123 // initializes it to "123"
} I think TypeScript would create the type right then and there, at class definition. The user cannot (write any code that will) Another thing could be that maybe decorators can augment the type of a class, but not necessarily the type of the thing they decorate directly. For example, this class Foo {
@withDouble count = 123
} could add a new However, I think that with this new decorator API, decorator functions can be completely generic, with their input and output types completely known by TypeScript. So, this should be possible: function alwaysString<T, C extends ...>(_: T, context: C): (initial: T) => string {
if (context.kind !== 'field') return // type narrowing based on union of string values for `kind`
return initial => initial.toString()
} and based on something like this, I imagine it totally possible for TypeScript to look at the return type and determine that the final type of the property should always be a But a version with class types not modifiable would still be totally usable! |
I suggest implement decorators first, then add type support. The current legacy class decorator could return a non-class constructor value, and it won't be recognized by ts type system, but it is still easy to use. Or we just use legacy version until stage 4 published, but maybe they are waiting us to do something to verify their design in stage 3. |
As long as |
I wonder if the narrow case of initializer mapping might be more doable than other kinds of changes caused by decorators. |
This is kind of what I've been thinking recently. I can imagine a world where
It's enough to help with auto-initialization (which is just removing The reason for the latter bullet is being able to support something along the lines of #47947, along with some other precedent we have (e.g. async functions need to say they return a |
This part works: class Foo {
@reactive array = [1, 2, 3] // converts to ReactiveArray<number> for example
} But what about this? const f = new Foo
f.array = [4, 5, 6] // Should also accept a plain array and convert to a (or map to the) `ReactiveArray<number>`
f.array // `ReactiveArray<number>` |
Also what about the case of a totally different but valid type? The following is based on real-world examples, namely for attribute processing with custom elements: import {numberArray} from 'some-custom-element-lib'
class MyEl extends HTMLElement {
// also accepts strings (f.e. from HTML attributes)
@numberArray coords = "1 2 3" // converts to ReactiveArray<number> for example
}
customElements.define('my-el', MyEl)
const el = new MyEL
el.array = [4, 5, 6] // Should also accept a plain array and convert to a (or map to the) `ReactiveArray<number>`
el.array = "7 8 9" // Should also accept a string with space-separate list of numbers and convert to a (or map to the) `ReactiveArray<number>`
el.array // `ReactiveArray<number>` |
Hmm, now that I think about it, maybe an Example: // some-custom-element-lib
export function numberAttribute() {
return {
get(): ReactiveArrayTriplet<number> {
// ...
},
set(val: `${number} ${number} ${number}` | [number, number, number] | ReactiveArrayTriplet<number>) {
// ...
},
}
} import {numberArray} from 'some-custom-element-lib'
class MyEl extends HTMLElement {
// also accepts strings (f.e. from HTML attributes)
@numberArray accessor coords = "1 2 3" // converts to ReactiveArray<number> for example
} In this case, the type of the implied getter/setter can be determined from the decorator return value. There's actually a way to write the above without using the import {element, numberArray} from 'some-custom-element-lib'
@element // this added decorator is needed for creating reactive accessors in a returned subclass based on decorated fields
class MyEl extends HTMLElement {
@numberArray coords = "1 2 3"
} but then I don't see how TypeScript would be able to determine differing get/set types for |
@trusktr Maybe that should be write in grouped accessors. class Foo {
accessor coords{
get():ReactiveArrayTriplet<number>{
//...
};
set(data:`${number} ${number} ${number}` | [number, number, number] | ReactiveArrayTriplet<number>){
//...
};
}
} |
@wycats In my opinion, the accessor type should be declared in the class. Decorators should be compatible with accessor type.
Decorators shouldn't break the type defined in class. That's also a typescript way to implement. |
Is there any information about TS plans to implement decorator extensions? Many real use cases require exactly the extended capabilities of decorators. |
@uasan I'm sure TypeScript wouldn't implement them before they're standardized, so it's really a question for TC39 and the decorators proposal champions. |
That defeats the purpose of having decorators: to write concise code and not repeat that pattern on every property. We are here in this thread to use decorators! |
For the time being, it would be great to have an option to not compile decorators (just like we can leave JSX untouched), so that we can pass output code along to other tools like Babel. This would be a lot simpler to implement, and still useful even after TypeScript gets updated decorators. |
Along with a |
Currently we can make this work using This does not work: export // export is required before stage-3 decorators, currently an error in TS
@stage3Decorator
class Foo {} This works: @stage3Decorator
class Foo {}
export {Foo} Your babel config plugins will look something like this: plugins: [
['@babel/plugin-transform-typescript', {...}],
['@babel/plugin-proposal-decorators', {version: '2022-03'}],
['@babel/plugin-proposal-class-static-block'], // needed, or decorator output will currently not work in Safari.
], and off to the races! 🐎 |
For now, here's what the signature of a stage 3 decorator may look like to avoid type errors: export function someDecorator(...args: any[]): any {
const [value, {kind, ...etc}] = args as [DecoratedValue, DecoratorContext]
console.log('stage 3!', value, kind, etc)
if (kind === 'class') {
// ...
} else if (kind === 'field') {
// ...
} else (/*...etc...*/) {
// ...
}
// ...
}
interface DecoratorContext {
kind: 'class' | 'method' | 'getter' | 'setter' | 'field' | 'accessor'
name: string | symbol
access: Accessor
private?: boolean
static?: boolean
addInitializer?(initializer: () => void): void
}
interface Accessor {
get?(): unknown
set?(value: unknown): void
}
type Constructor<T = object, A extends any[] = any[], Static = {}> = (new (...a: A) => T) & Static
type DecoratedValue = Constructor | Function | Accessor | undefined |
Sorry for the offtopic question, but what will happen to stage3 decorators? Will they be removed from Typescript? |
I assume you mean the Stage 1 "experimental" decorators that are already in TypeScript? That will remain behind
We also do not currently support decorated |
The built-in lib definitions in #50820 contain types that will hopefully be able to help: // class decorator
function MyClassDecorator<T extends new (...args: any) => any>(target: T, context: ClassDecoratorContext<T>) {
};
// method decorator
function MyMethodDecorator<T extends (...args: any) => any>(target: T, context: ClassMethodDecorator<unknown, T>) {
};
// or, a catch-all decorator
function MyDecorator(target: unknown, context: DecoratorContext) {
switch (context.kind) {
case "class": ...
case "method": ...
...
}
} |
@rbuckton, thank you, I mean ES decorators reached stage 3. And because of that, I believe they'll be implemented in the Typescript as well. And correct me if I'm wrong, but the newest decorators are not compatible with the good old "experimental". So I thought, Typescript will have to support them since a lot of frameworks are relying on them. |
Hope there is adapter to update experimental decorators to stage 3 automatic. |
@pokatomnik while experimental stage 1 decorators will be supported for some unknown amount of time, I believe we can assume that eventually non-experimental stage 3 decorators will land and we'll be able to rely on those. If you want to prepare for this future, the best way to do that currently is to use I'm betting on stage 3 decorators with a setup like I mentioned in the above comment. |
I don't think TypeScript will do that (I don't think it has ever been done for any feature). Experimental stage 1 decorators and stage 3 decorators are highly incompatible. Start migrating! |
I will try, but it is a terrible work especially for package creators to update their user's code. |
A little nod about the metadata in decorators. tc39/proposal-type-annotations#159 can this be done in typescript? |
No, we won't automatically adapt legacy decorators as that would require type-directed emit. Also, there is not 1:1 parity between legacy decorators and ECMAScript decorators, which I mentioned above. It's possible to write a custom decorator adapter, though that would entail varying degrees of complexity depending on the decorator you are adapting. An adapter that provided full backwards compatibility with legacy decorators would likely incur a performance penalty, though it would be feasible: // legacy.ts
@foo
@bar({ x: 1 })
class C {
@baz method() {}
@quxx field;
}
// native.ts
import { createDecoratorAdapter } from "./adapter";
const _ = createDecoratorAdapter();
@_ // <- last applied decorator
@_(foo)
@_(bar({ x: 1 })
class C {
@_(baz) method() {}
@_(quxx) field;
}
// adapter.ts
export function createDecoratorAdapter() {
// TODO: returns a function that queues up each legacy decorator application
// and performs legacy decorator evaluation at the end as part of a
// final class decorator.
} |
Here's a rough implementation of a comprehensive NOTE: It's not designed to be intermingled with native decorators, as all decorator applications are queued until an adapted class decorator runs (or the adapter itself is used as a class decorator). |
Suggestion
Implement Decorators!
🔍 Search Terms
TypeScript Decorators
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
Decorators
✅ Viability Checklist
My suggestion meets these guidelines:
Not sure - this would be badly breaking against your legacy decorators feature, but I think it meets the goal to align TS with JS
⭐ Suggestion
The TypeScript proposal is now Stage 3!!!
https://github.com/tc39/proposal-decorators
We've all seen the issue from hell requesting better typing support for decorators
#4881
but now that the proposal is moving forward, currently at Stage 3, I thought I'd open this issue to see if there are plans to finally, fully implement (and type) the ES version of decorators.
📃 Motivating Example
N/A - just implementing new JS features, to keep JS and TS aligned.
💻 Use Cases
All the various ways decorators can be used.
The text was updated successfully, but these errors were encountered: