In TypeScript 5 the property decorator's type definition has changed and there does not seem to be a way to get a class's constructor.
It now implements the TC39 proposal where the first parameter to a property decorator is always undefined
:
type ClassFieldDecorator = (value: undefined, context: {
kind: "field";
name: string | symbol;
access: { get(): unknown, set(value: unknown): void };
static: boolean;
private: boolean;
}) => (initialValue: unknown) => unknown | void;
While previously, the first parameter to the decorator function was the class's constructor:
function myPropertyDecorator(target: Object, propertyKey: string) {
...
}
An ability to get a class's constructor was very useful for some libraries, e.g. validation libraries, that were operating on a class level and were storing a map of validations per class.
What are the options for doing this with the new decorator's proposal?
- Get a class constructor in the initialization function?
- Use
experimentalDecorators
instead of new decorators? - Something else?
You can have access to the class instance in the callback function of the decorator :
Make sure you don't use an arrow function to not keep the context of the decorator function.
Playground