Luka Šilje Luka Šilje - 1 month ago 14
TypeScript Question

Is there a Typescript equivalent to C#'s attributes

In C# there is a syntax available to define attributes of a property.

[Required]
string personName


It describes that personName is required. We can get the attributes of a property in any given time via reflection.

I was wondering if Typescript has some feature like that?

Answer

I was wondering if Typescript has some feature like that?

Decorators are like that. E.g. mobx (https://github.com/mobxjs/mobx) uses it to make things observable.

class TodoList {
    @observable todos = [];
    @computed get unfinishedTodoCount() {
        return this.todos.filter(todo => !todo.finished).length;
    }
}