搜索词
"无法解析属性装饰器的签名,当作为表达式调用时"
建议
我收到的错误信息是 error TS1240: Unable to resolve signature of property decorator when called as an expression.
这完全没有任何线索表明出了什么问题。
示例
要解决这个问题,我不得不将其中一个函数的签名从
export function attribute(
prototype: any
propName: string,
descriptor: PropertyDescriptor | undefined,
): any
更改为
export function attribute(
prototype: any
propName: string,
descriptor?: PropertyDescriptor | undefined,
): any
区别微妙:descriptor
参数并不总是传递给装饰器(取决于它是否用于属性、访问器或方法)。
如果消息能说一些类似 The 'descriptor' argument to a property decorator should not be required.
的话就更好了。
对于触发相同无助错误信息的其它情况也是如此。
检查清单
我的建议满足以下准则:
- 这不会对现有的 TypeScript/JavaScript 代码造成破坏性改变
- 这不会改变现有 JavaScript 代码的运行时行为
- 这可以在不根据表达式的类型发出不同的 JS 的情况下实现
- 这不是一个运行时特性(例如库功能、带有 JavaScript 输出的非 ECMAScript 语法等)
- 这个特性会与 TypeScript's Design Goals 中的其余部分保持一致。
4条答案
按热度按时间qhhrdooz1#
你能给出一个具体的例子来说明这个函数的用法吗?
w8ntj3qf2#
我找到了两个与此错误相关的测试,但可能还有一些特定情况。
TypeScript/tests/cases/conformance/decorators/class/property/decoratorOnClassProperty7.ts
第3行到第7行:57e2fe0
| | declarefunctiondec(target: Function,propertyKey: string|symbol,paramIndex: number): void; |
| | |
| | classC{ |
| | @decprop; |
| | } |
TypeScript/tests/cases/conformance/decorators/class/property/decoratorOnClassProperty6.ts
第3行到第6行:57e2fe0
| | declarefunctiondec(target: Function): void; |
| | |
| | classC{ |
| | @decprop; |
v2g6jxz63#
看起来像是#4534的重复内容。
l3zydbqr4#
如要求,这是在操场上的一个小复制品。