Type inference worked in 4.2.3 but fails in 4.3.2

Aperta
#44,412 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

Inizia con il repro collegato in TypeScript Playground e confronta l’inferenza tra le versioni 4.2.3 e 4.3.2, concentrandoti sulla scelta di null nell’array choices e sulla chiamata a promptChoice. Il lavoro è completato quando l’esempio non segnala più l’errore di assegnazione della tupla e choice viene inferito come 'one'|'two'|'three'.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 15h
PR unite (30g)
106

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/TypeScript

Tutte le issue di microsoft/TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.