microsoft / microsoft/TypeScript
Either understand const arrow assertions or give better errors on them
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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