microsoft / microsoft/TypeScript

Create subtype reduction rules for assertion functions

Open
#53,963 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experimentation Needed Suggestion
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
⏯ 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; }))[]): void
  • function Test<(a: any) => asserts a is number>(params: ((a: any) => asserts a is number)[]): void
  • function Test2<(((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]>(params: (((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]): void
  • function 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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.