I tried to apply the @Reflect.metadata
decorator to a TypeScript class, following the example on lines 82-84 of reflect-metadata.d.ts :
/// <reference path="node_modules/reflect-metadata/reflect-metadata.d.ts"/>
@Reflect.metadata('key', 0)
class C {
}
However, the TypeScript 1.7.2 compiler generates the following error on the @Reflect.metadata
line:
error TS1238: Unable to resolve signature of class decorator when called as an expression.
Cannot invoke an expression whose type lacks a call signature.
What's wrong?
From the TypeScript docs :
Decorators are checked as call expressions
Starting with 1.6, decorators type checking is more accurate; the compiler will checks a decorator expression as a call expression with the decorated entity as a parameter. This can cause error to be reported that were not in previous releases.
My guess is that you probably need to use a newer version of TypeScript or an older version of reflect-metadata
.
The latest releases are:
typescript@2.3.2
reflect-metadata@0.1.10
The solution is to mark all of the decorator function's arguments as optional:
function logType(a?: any, b?: any) {
console.log(a, b);
}
@logType
class MyClass {...}
and then adjust them according to your needs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.