microsoft / microsoft/TypeScript
Create subtype reduction rules for assertion functions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
inference assert
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs
⏯ Playground Link
Playground link with relevant code
💻 Code
function Test<X extends any>(params: X[]) {}
function Test2<InParams extends readonly any[]>(params: InParams) {}
function f1(a: any): number {return 1}
function f2(a: any): string {return "string"}
function f3(a: any) {return {k: 1}}
function a1(a: any): asserts a is number {}
function a2(a: any): asserts a is string {}
function a3(a: any): asserts a is {k: number} {}
const aifwe = [f1, f2, f3]
Test([f1, f2, f3])
Test([a1, a2, a3])
Test2([f1, f2, f3])
Test2([a1, a2, a3])
🙁 Actual behavior
The Test and Test2 calls are inferred as (in order):
function Test<((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; })>(params: (((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]): voidfunction Test<(a: any) => asserts a is number>(params: ((a: any) => asserts a is number)[]): voidfunction Test2<(((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]>(params: (((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]): voidfunction Test2<((a: any) => asserts a is number)[]>(params: ((a: any) => asserts a is number)[]): void
🙂 Expected behavior
For the assert calls to also be inferred as unions.
This is also irregular since with the wrong inference, I would expect the other functions to be marked as errors, since they have different type signatures.
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
Start with the linked TypeScript Playground example and trace generic inference for arrays of assertion functions, focusing on the requested subtype reduction rules. Done when the Test and Test2 assertion calls infer unions like the ordinary function calls, with the relevant inference behavior covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100