这里使用新的ECMAScript装饰是我的代码示例:不能在打字稿2.4.2
function enumerable(value: boolean) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
descriptor.enumerable = value;
};
}
class A {
@enumerable(false)
a: number = 1
b: number = 2
myMethod() {}
}
const a = new A()
无论我尝试,我得到:
D:(real path removed)/first-try-typescript>tsc --emitDecoratorMetadata --experimentalDecorators decorators.ts
decorators.ts(8,3): error TS1240: Unable to resolve signature of property decorator when called as an expression.
我已经尝试了从同一stackoferflow问题的建议:
- 加入emitDecoratorMetadata & experimentalDecorators到tsconfig
- 运行TSC --emitDecoratorMetadata --experimentalDecorators
- 加入
:any
标记装饰函数返回值 - 加入
descriptor: TypedPropertyDescriptor<any>
型
我总是得到这个错误。在终端和Webstorm代码提示中。方法装饰器 - 同样的事情(见下面的例子)。
function test(target: Object,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>): any {
return descriptor;
}
class A {
a: number = 1
b: number = 2
@test
myMethod() {}
}
const a = new A()
到目前为止代码是在这里 - https://github.com/rantiev/first-try-typescript
什么是你想要的结果?装饰器应该应用于函数/方法,你正在试图做似乎是混合的方法为一类 –
@NickTomlin - 这是不正确,装饰器可以应用到一个属性为好。 –
@JohnWeisz啊有趣,我的坏话呢! –