microsoft / microsoft/TypeScript

Deprecate homomorphic mapped type over primitive

Aperta
#56,940 10 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

homomorphic mapped primitive

✅ Viability Checklist
⭐ Suggestion

I would like homomorphic mapped types over primitives to emit a warning if the base type is not an object type. An explicit conditional type Foo<T> = T extends object ? { [K in keyof T]: ...} : T can be used to recover the pre-existing behavior. This should be mentioned in the warning message.

Homomorphic mapped type over primitives has surprising behavior that bears mention in the FAQ:

  • This mapped type returns a primitive type, not an object type.
    • Mapped types declared as { [ K in keyof T ]: U } where T is a type parameter are known as homomorphic mapped types, which means that the mapped type is a structure preserving function of T. When type parameter T` is instantiated with a primitive type the mapped type evaluates to the same primitive.

This behavior is surprising since:

  1. It is not referentially transparent; that is, replacing a type parameter with its value does not result in the same type
  2. Although a mapped type may be assignable to the original type, it may specifically not be, e.g. {[k in keyof T]: never} whereas it always is in the case of primitives.
  3. The expression keyof T has surprising behavior on primitive types, differing substantially from object types.
  4. The syntax of a mapped type visually suggests an object-like type, which is incorrect when mapping over null.
📃 Motivating Example
type NullValued<X> = {[k in keyof X]: null};
type Okay = NullValued<{x:1, y:2}>; // {x:null, y:null}
type Oops = NullValued<number>; // oops! This is `number`, which is likely not what you expected
💻 Use Cases
  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  • You have to know about homeomorphic mapped types and anticipate this corner case.
  1. What workarounds are you using in the meantime?
  • A type guard type Foo<T extends object> = { [K in keyof T]: ...} guards against the current behavior.
  • A conditional type Foo<T> = T extends object ? { [K in keyof T]: ...} : T makes the current behavior explicit.

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 l’esempio motivante NullValued e il passaggio della FAQ nell’issue; non viene indicato alcun file di implementazione né alcun test. Il lavoro è completo quando gli homomorphic mapped types su primitive bases producono l’avviso richiesto e l’avviso spiega l’explicit conditional workaround, mentre il comportamento sorprendente è documentato nella FAQ.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.