-
Notifications
You must be signed in to change notification settings - Fork 12.8k
A more sensible error message for assertions on arrow functions #53450
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
Comments
But arrow-functions can be assertions. They just need an explicit type annotation, as mentioned in TS2775. |
Ah, this is what I get for only reading the title and then just scrolling all the way down |
That error pops up where I call that arrow function though, not where I declare it. Making me think I need to change something about the way I call it, or that the parameter I'm inserting is somehow wrong. I think this is part of the confusion. |
The error message you're getting is telling you what's wrong
// Not annotated
const expectFoo = ...
// Annotated
const expectFoo: SomeType = ... I'm open to a correct rewording of this error message if anyone has one. |
How come thereβs not an equivalent error for |
I also don't know what this is. Is the call target the function that is called? Why are there multiple "names" in the function? My 3.5 years of javascript and half a year of typescript experience are not enough to understand this error. Idk. Am I not good enough? Or is this error message a bit too funky |
Given that these are the rules, Iβm not sure how to make the error any clearer myself. It has to do with the structure of the call site so is kind of technical in nature. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
Lost one hour of my life because of this confusing error. please re-open the issue |
+1 to re-opening this. Needing to explicitly annotate the actual variable apart from its value is something that, AFAIK, doesn't exist anywhere else in TS. When I read the error, I read it as "arguments need to be annotated", and my internal response is "Well, they are...": Then I had to find #34523 to figure out exactly what the issue actually was. const foo = (x: string): asserts whatever => { ... } contains exactly the same info as const foo: (x: string) => asserts whatever = (x) => {} (and if you inspect them TS returns the same types) So if the issue is with internal complexity, cool, but the error should actually clarify what the issue is and how to fix it. |
Uh oh!
There was an error while loading. Please reload this page.
Suggestion
π Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
This line should show a useful error message, like
TSxxxx: Assertions cannot be used on a arrow-functions
π Motivating Example
π» Use Cases
Instead it shows
TS2775: Assertions require every name in the call target to be declared with an explicit type annotation.
as error. And not where theassert
is, but instead where the function is called.#34523 (comment)
I also think this is extremely confusing. I just had to look that error up for the third time now, visiting the same StackOverflow post for the third time, and realizing that I had assertions on an arrow function again for the third time.
An error like
TSxxxx: Assertions cannot be used on an arrow-function
would be much better.The text was updated successfully, but these errors were encountered: