@Reflect.metadata generates error TS1238: Unable to resolve signature of class decorator when called as an expression
Asked Answered
N

2

6

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?

Nehemiah answered 28/12, 2015 at 20:27 Comment(0)
S
0

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:

Scathing answered 7/5, 2017 at 0:17 Comment(0)
R
0

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.

Reduction answered 4/1, 2020 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.