Skip to content

[feature request] Better error messages for decorators (they are completely not understandable) #39991

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

Open
5 tasks done
trusktr opened this issue Aug 11, 2020 · 5 comments
Open
5 tasks done
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Milestone

Comments

@trusktr
Copy link
Contributor

trusktr commented Aug 11, 2020

Search Terms

"Unable to resolve signature of property decorator when called as an expression"

Suggestion

The error message I received was error TS1240: Unable to resolve signature of property decorator when called as an expression.

This leaves absolutely no clue what is wrong.

Examples

To fix the issue, I had to change the signature of one of my functions from

export function attribute(
	prototype: any
	propName: string,
	descriptor: PropertyDescriptor | undefined,
): any

to

export function attribute(
	prototype: any
	propName: string,
	descriptor?: PropertyDescriptor | undefined,
): any

The difference is subtle: the descriptor arg isn't always passed to a decorator (depends on whether it is used on a property or on an accessor or method).

It'd be better if the message said something like The 'descriptor' argument to a property decorator should not be required.

Similar for other cases that trigger the same unhelpful error message.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@trusktr trusktr changed the title Better error messages for decorators (they are completely not understandable) [feature request] Better error messages for decorators (they are completely not understandable) Aug 11, 2020
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Aug 11, 2020
@DanielRosenwasser
Copy link
Member

Can you give a specific example of the usage of that function?

@a-tarasyuk
Copy link
Contributor

I found two tests related to this error, however, maybe there is some specific case.

declare function dec(target: Function, propertyKey: string | symbol, paramIndex: number): void;
class C {
@dec prop;
}

declare function dec(target: Function): void;
class C {
@dec prop;

@a-tarasyuk
Copy link
Contributor

It seems duplicate of #4534

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 11, 2020
@trusktr
Copy link
Contributor Author

trusktr commented Nov 3, 2020

As requested, here's a small reproduction on playground.

@Alexandrialexie
Copy link

Alexandrialexie commented Nov 18, 2024

It seems like this problem was resolved in v5.0.4. The error message gives more context now. It now reads:

Unable to resolve signature of property decorator when called as an expression.
The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.(1240)

input.tsx(4, 2): An argument for 'descriptor' was not provided."

Are any more changes to the error message still needed?

Playground link

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 Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

5 participants