@jmalarcon/

TSPropertyDecorator

TypeScript

No description

fork
loading
Files
  • index.ts
  • tsconfig.json
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function startAfterZero(target: Object, propertyKey: string) {
  let val = target[propertyKey];

  const getter = () => val;

  const setter = newVal => {
    if (newVal < 0) {
      throw new Error('Negative numbers are not allowed.');
    }

    val = newVal + 1;
  }

  if (delete target[propertyKey]) {
      Object.defineProperty(target, propertyKey, {
          get: getter,
          set: setter
      });
  }
}

class Human {
  @startAfterZero
  age: number;

}

const person = new Human();
person.age = 10;
console.log(person.age);