Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for decorator type mismatches #4534

Open
weswigham opened this issue Aug 28, 2015 · 9 comments
Open

Improve error message for decorator type mismatches #4534

weswigham opened this issue Aug 28, 2015 · 9 comments
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Milestone

Comments

@weswigham
Copy link
Member

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.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 1, 2015

Looks like a fair proposal. feel free to send a PR if you like.

@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this labels Sep 1, 2015
@mhegazy mhegazy added this to the Community milestone Sep 18, 2015
@aciccarello
Copy link

Something similar for TS1238: Unable to resolve signature of class decorator when called as an expression. would also be helpful

@stepancar
Copy link

Hello! Whats status of this issue?

@DanielRosenwasser DanielRosenwasser added the Good First Issue Well scoped, documented and has the green light label Jan 24, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2016

it is marked as "Accepting PRs", so PRs are welcomed :)

@origin1tech
Copy link

As of the date of this post this still seems to be an issue.

The following:

const typeMetadataKey = Symbol('type');

function type(name: string): PropertyDescriptor {
 return Reflect.metadata(typeMetadataKey, name);
}

Produces the error: Unable to resolve signature of class decorator when called as an expression..

When used as below:

class Person {
  @type('string')
  firstName: string;
}

The decorator does work as expected and will compile but gives the error above.

If you have thoughts on how this might be resolved happy to dig into it if someone would like to point to the right direction.

@stewhouston
Copy link

stewhouston commented May 20, 2016

This may be tangential, but I had a tough time finding a solution. I was able to create a class factory by casting the return function. Not sure if this is a valid design pattern, but I am using it for dependency injection similar to what is seen in Angular2.

return function(decoratedClass:any) {
    function wrapConstructor(app) {
        return new decoratedClass(app, ...providers);
    }

    wrapConstructor.prototype = decoratedClass.prototype;

    return <typeof decoratedClass> wrapConstructor;
}

@lizzzp1
Copy link

lizzzp1 commented Feb 19, 2019

Could I take this one?

@dderpym
Copy link

dderpym commented Sep 10, 2024

Looks like there hasn't been any activity on this, could I take a crack at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

10 participants