종류

Decorator는 전부 function이다. 그리고 어디에 쓰이는지 종류에 따라 시그니쳐 (약속) 이 다르다.

Class Decorator

function hello(constructorFn: Function) {
  console.log(constructorFn);
}

@hello
class Person {
}
function hello(constructorFn: Function) {
  console.log(constructorFn);
}

function helloFactory(show: boolean) {
  return show ? hello : () => {};
}

@helloFactory(false)
class Person {

}
function hello(constructor: Function) {
  constructor.prototype.hello = function () {
    console.log(this.name, 'hello');
  }
}

@hello
class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

const a = new Person('max');
(<any>a).hello();

Method Decorator

function editable(canBeEditable: boolean): MethodDecorator {
  return function(target, propertyKey, descriptor) {
    console.log(target);
    console.log(propertyKey);
    console.log(descriptor);

    descriptor.writable = canBeEditable;
  }
}

class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
    console.log('new Person()');
  }

  @editable(true)
  hello() {
    console.log('hello!!!', this.name);
  }
}