microsoft / microsoft/TypeScript

{} and { [K in never]: any } exhibit different simplification behavior

Aperta
#42,864 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

Under some circumstances, TypeScript treats { [K in never]: any } & { foo?: number } differently than {} & { foo?: number }. However, this does not occur with { [K in never]: any } & { foo: number }.

🔎 Search Terms

empty mapped object type, intersection with optional object, K in never

🕗 Version & Regression Information

Tested on 4.0.5, 4.1.5, 4.2.0-beta, and the current nightly

⏯ Playground Link

Playground link

💻 Code
type AreAllStringsTs<T> = string extends T ? true : false;
type Test1 = AreAllStringsTs<{ foo?: number }>;                         // expected: false, actual: false
type Test2 = AreAllStringsTs<{ foo: number }>;                          // expected: false, actual: false
type Test3 = AreAllStringsTs<string | number>;                          // expected: true,  actual: true
type Test4 = AreAllStringsTs<{ [K in never]: any } & { foo?: number }>; // expected: false, actual: true  <-- FAIL
type Test5 = AreAllStringsTs<{ [K in never]: any } & { foo: number }>;  // expected: false, actual: false
🙁 Actual behavior

Test4 is true.

🙂 Expected behavior

Test4 is false.

Potential Workaround

If you have a mapped object type in which the type expression for the keys might reduce to never, add a conditional check to see if the keys set is empty. For example, I encountered this when working with a type that would make all keys of an object that could take on the value undefined optional:

type Optionalize<T> =
    { [K in RequiredKeys<T>]: T[K] }
    & { [K in NonRequiredKeys<T>]?: T[K] };

Adding a check for never made the behavior consistent with what I was expecting when every field of the input type accepted undefined:

type Optionalize<T> =
    (RequiredKeys<T> extends never ? {} : { [K in RequiredKeys<T>]: T[K] })
    & { [K in NonRequiredKeys<T>]?: T[K]; };

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 link Playground fornito e riproduci la differenza tra Test4 e Test5. Traccia il modo in cui il compilatore semplifica il tipo mappato vuoto quando viene intersecato con tipi oggetto opzionali e obbligatori. Il lavoro è completato quando Test4 viene valutato come false, mentre le altre aspettative riportate rimangono invariate.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.