Can I apply a TypeScript decorator to all fields of a class?

I worked with decorators in Typescript, and I have a simple problem, suppose I have my own decorator called RetainTypeand a class like this:

class Person {
    @RetainType name: string;
    @RetainType age: number;
    @RetainType dateOfBirth: Date;
}

I would like to write:

@RetainType class Person {
    name: string;
    age: number;
    dateOfBirth: Date;
}

In other words, is there a way to apply a decorator to all the properties of a class? I use @RetainTypeto extract metadata about individual properties (design: type specific). It would be nice to have a more concise way to do this than annotate all the fields one by one.

+4
source share
1 answer

A property decoder applies to a single property. It does not return anything, just do some things with a related property.

. , .

- , . , , , . , . .

+1

Source: https://habr.com/ru/post/1651587/


All Articles