typescript-book-chinese icon indicating copy to clipboard operation
typescript-book-chinese copied to clipboard

用 Decorator 限制函数返回参数的类型

Open jkchao opened this issue 5 years ago • 1 comments

如下代码所示:

const TestDecorator = () => {
  return (
    target: Object,
    key: string | symbol,
    descriptor: TypedPropertyDescriptor<() => number>   // 函数返回值必须是 number
  ) => {
    // ...
  }
}

class Test {
  @TestDecorator()
  testMethod() {
    return '123';   // Error: '123' 是 string 类型
  }
}

jkchao avatar Nov 26 '18 02:11 jkchao

cool

DricHao avatar Apr 22 '19 11:04 DricHao