Skip to content

Improve error message for decorator type mismatches #4534

Open
@weswigham

Description

@weswigham

In TS 1.5 this:

//Relation.ts
export function Relation(cls: any) {
    return function () {
        console.log(cls);
    }
}

//Foo.ts
import {Relation} from "./Relation";

export class Foo {
    @Relation(Foo)
    info: string;
}

Compiles without error. In TS 1.6, it reports the error:

error TS1240: Unable to resolve signature of property decorator when called as an expression.
  Supplied parameters do not match any signature of call target.

On the decorator call in Foo.ts. This is a very cryptic message. What it wants you to do is change Relation.ts to read like this:

export function Relation(cls: any): PropertyDecorator {
    return function () {
        console.log(cls);
    }
}

or this:

export function Relation(cls: any) {
    return function (target: Object, propertyKey: string | symbol) {
        console.log(cls);
    }
}

It would be very useful for the error message to indicate what exactly is wrong and how it could be fixed. Something akin to

error TS1240: Expression type is not assignable to decorator type [[PropertyDecorator]]. Ensure [[@Relation(Foo)]] has a type assignable to [[PropertyDecorator]].

would be more useful in actually understanding what's wrong and why you need to change your code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions