microsoft / microsoft/TypeScript

Two way assignability condition with generic params is not working as expected

Aperta
#44,975 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Conditional Types Needs Human Review
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

assignability, ternary, conditions, generics

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
function f<G>() {
  const x: NumberIfExtends<G, G> = 1;
}
🙁 Actual behavior

The value 1 is not assignable to variable x, with the following error: Type 'number' is not assignable to type 'NumberIfExtends<G, G>'.

🙂 Expected behavior

This should be assignable, since the the value passed to both T and U generic params is the same.

Please note the following very similar examples, that works without errors.
This is to show this is a minimal reproduction, and that the different do not justify the error:

type NumberIfExtends<T, U> = T extends U ? U extends T ? number : string : string;
const x: NumberIfExtends<'some type', 'some type'> = 1; // if I pass an explicit type (Not a generic)
type NumberIfExtends<T, U> = T extends U ? number : string; // if I use the condition in only one way. 
function f<G>() {
  const x: NumberIfExtends<G, G> = 1;
}
function f<G>(): void {
  const x: G extends G ? G extends G ? number : string : string = 1; // if I define the type without an alias. This is not always possible if instead of defining a variable I would be passing the value to a function who defines its own type
}

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.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e la riproduzione minima di NumberIfExtends<G, G>. Traccia come viene verificata l'assegnabilità dei tipi condizionali con parametri generici, confrontando la forma alias con gli esempi inline e unidirezionale. Il lavoro è completato quando l'assegnazione di 1 al tipo condizionale generico riesce senza l'errore riportato.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.