Ale*_*huk 10 annotations decorator typescript
如何使用TypeScript和装饰器定义属性?
例如,我有这个类装饰器:
function Entity<TFunction extends Function>(target: TFunction): TFunction {
Object.defineProperty(target.prototype, 'test', {
value: function() {
console.log('test call');
return 'test result';
}
});
return target;
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
@Entity
class Project {
//
}
let project = new Project();
console.log(project.test());
Run Code Online (Sandbox Code Playgroud)
我有这个控制台日志:
test call entity.ts:5
test result entity.ts:18
Run Code Online (Sandbox Code Playgroud)
此代码正常工作,但tsc返回错误:
entity.ts(18,21): error TS2339: Property 'test' does not exist on type 'Project'.
Run Code Online (Sandbox Code Playgroud)
如何解决这个错误?
| 归档时间: |
|
| 查看次数: |
5188 次 |
| 最近记录: |