typescript-book-chinese
typescript-book-chinese copied to clipboard
用 Decorator 限制函数返回参数的类型
如下代码所示:
const TestDecorator = () => {
return (
target: Object,
key: string | symbol,
descriptor: TypedPropertyDescriptor<() => number> // 函数返回值必须是 number
) => {
// ...
}
}
class Test {
@TestDecorator()
testMethod() {
return '123'; // Error: '123' 是 string 类型
}
}
cool