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

Either understand const arrow assertions or give better errors on them #36067

Open
DanielRosenwasser opened this issue Jan 8, 2020 · 2 comments
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements In Discussion Not yet reached consensus Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

As mentioned by @threehams in #35838 (comment), people are pretty confused about the rules for assertion functions, and I don't blame them. For example

const assert = (blah: unknown): asserts blah => { throw "hai"; }

let x: string | undefined;;
assert(x);

In our nightly releases, we give an elaboration on the declaration of assert like

'assert' needs an explicit type annotation.

For all intents and purposes, it looks like assert does have an annotation! The problem is that assert itself needs a : (x: unknown) => asserts x, because it's not automatically inferred from the arrow function.

We have two options:

  • Remove this restriction when the declaration of an assertion function is trivially detectable.

  • Make assertion function errors more clear for arrow functions:

    'assert' needs an explicit type annotation. While the function it has been assigned to is fully annotated, the variable declaration for 'assert' does not.
    
@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Jan 8, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.9.0 milestone Jan 8, 2020
@DanielRosenwasser DanielRosenwasser added the In Discussion Not yet reached consensus label Jan 21, 2020
@DanielRosenwasser DanielRosenwasser added the Domain: Error Messages The issue relates to error messaging label Feb 4, 2020
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 20, 2020
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 4.0.1 milestone Jul 15, 2020
@flybayer
Copy link

This is still very confusing. Would be nice to get it fixed! :)

@ssalbdivad
Copy link

ssalbdivad commented Nov 10, 2023

I constantly get tripped up by this! Strong +1 for:

Remove this restriction when the declaration of an assertion function is trivially detectable.

It doesn't seem like there should be a lot of edge cases to handle here if it's required that the return type is explicitly annotated.

Also worth noting this affects never returning functions as well, see #56362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements In Discussion Not yet reached consensus Rescheduled This issue was previously scheduled to an earlier milestone Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants