microsoft / microsoft/TypeScript

Some types are incorrectly assignable from a generic distributive conditional type

Aperta
#32,066 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Conditional Types
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.5.1

Search Terms: generic conditional distributive assignable from

Code

type Keys<T extends object> = T extends unknown ? keyof T : never;

function f<T extends object>(keys: keyof T, moreKeys: Keys<T>) {
    keys = moreKeys;
}

// With this instantiation, `keys` is typed 'a',
// and `moreKeys` is typed 'a' | 'b' | 'c'. The latter
// should not be assignable to the former, but it happens
// in the generic context of the function `f`.
f<{ a: any, b: any } | { a: any, c: any }>('a', 'b');

Expected behavior:

Keys<T> should not be assignable to keyof T since Keys<T> is distributive and will produce a different type than keyof T when T is a union type.

Note that the reverse of this assignment, moreKeys = keys, errors for this exact reason; without a concrete T, it’s hard to tell what the relationship should be, so we simply have no relationship in place.

Actual behavior:

The unsafe assignment is allowed.

Playground Link

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 riproducendo l'esempio generico di tipo condizionale distributivo nel Playground collegato. Traccia il percorso del controllo dei tipi per l'assegnabilità nel contesto generico, quindi aggiungi un test di regressione che copra l'assegnazione non sicura e il comportamento di assegnazione inversa indicato. Il lavoro è completato quando la chiamata con 'b' viene rifiutata perché Keys non è assegnabile a keyof T.

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à
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.