Skip to content

A more sensible error message for assertions on arrow functionsΒ #53450

Closed
@sezanzeb

Description

@sezanzeb

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.

  • 2775
  • arrow

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

const expectFoo = (bar: unknown): asserts bar is Foo => { ... };

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 the assert is, but instead where the function is called.

#34523 (comment)

A better error message on this design limitation would have been nice instead of the confusing TS 2775

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.

Metadata

Metadata

Assignees

Labels

QuestionAn issue which isn't directly actionable in code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions