Type inference worked in 4.2.3 but fails in 4.3.2

Open
#44,412 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start with the linked TypeScript Playground repro and compare inference between versions 4.2.3 and 4.3.2, focusing on the null choice in the choices array and the promptChoice call. Done means the example no longer reports the tuple assignment error and choice is inferred as 'one'|'two'|'three'.

Written by the indexing model from the issue text.

Description

Bug Domain: check: Type Inference

Bug Report

🔎 Search Terms

type inference

🕗 Version & Regression Information

4.3.2

  • This is a crash
    [x] This changed between versions 4.2.3 and 4.3.2
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______
⏯ Playground Link

Playground link with relevant code

💻 Code

(strict null checks off)

interface PromptChoiceOptions<T extends string> {
	label: string;
	button: string;
	default?: T;
	choices: readonly ( T | [ string, T ] )[];
}

function promptChoice<T extends string>( options?: Partial<PromptChoiceOptions<T>> ) {
    return options?.choices?.[ 0 ] ?? null; // simplified
}

const choice = promptChoice( { label: 'example', button: 'select', choices: [
    [ 'null', null ], // problem seems to be related to this line
    [ 'One', 'one' ],
    [ 'Two', 'two' ],
    [ 'Three', 'three' ]
] } );
🙁 Actual behavior

Type [string, "two"] is not assignable to type [string, "one"]

🙂 Expected behavior

choice is inferred to be 'one'|'two'|'three'


In creating a minimal repro for this, I discovered that commenting out the null choice makes the code behave properly. Also, changing the type of choices to readonly ( T | [ string, T|null] )[]; with strict null checking makes the problem always occur.

Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

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.

More from microsoft/TypeScript

All issues in microsoft/TypeScript

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.