[컴] Typescript 의 Decorator

 

annotation / 어노테이션 / 데코레이터 /

Typescript 의 Decorator

function first() {
  console.log("first(): factory evaluated");
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    console.log("first(): called");
  };
}
 
function second() {
  console.log("second(): factory evaluated");
  return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    console.log("second(): called");
  };
}
 
class ExampleClass {
  @first()
  @second()
  method() {}
}
first(): factory evaluated
second(): factory evaluated
second(): called
first(): called

build

아래처럼 --experimentalDecorators 를 해줘야 한다. 그렇지 않으면 error 가 생긴다.

tsc --experimentalDecorators

See Also

  1. 쿠…sal: [컴] 간단하게 typescript 시작하기 2
  2. 쿠…sal: [컴][TypeScript] typescript 기본 setup + visual studio code
  3. 쿠…sal: [컴][디자인패턴] Decorator Pattern
  4. 쿠…sal: [컴][파이썬] python decorator

Reference

  1. TypeScript: Documentation - Decorators

댓글 없음:

댓글 쓰기