microsoft / microsoft/TypeScript

Either understand const arrow assertions or give better errors on them

Open
#36,067 2 comments 27 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Error Messages Experience Enhancement In Discussion Rescheduled Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

As mentioned by @threehams in https://github.com/microsoft/TypeScript/issues/35838#issuecomment-569832017, 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.
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Reproduce the const arrow assertion example from the issue in a nightly TypeScript build, then inspect the existing assertion-function diagnostic path. Done means either trivially detectable arrow assertions are accepted or the diagnostic explains that the variable declaration itself lacks the required explicit type annotation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.