microsoft / microsoft/TypeScript

Certain homomorphic mappings break assignability with exactOptionalPropertyTypes

Aperta
#60,233 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: flag: exactOptionalPropertyTypes Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

eopt exactOptionalPropertyTypes homomorphic Omit unassignable

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

https://tsplay.dev/w28Qzm

💻 Code
// @exactOptionalPropertyTypes

type T = {
  foo?: true;
  bar?: true;
};

type WrappedT<t extends T> = [t];

// Since Omit is normally treated as homomorphic, should work but errors with:
// Type 'true | undefined' is not assignable to type 'true'.
type OmitBarFromWrapped<t> =
  t extends WrappedT<infer inner> ? WrappedT<Omit<inner, "bar">> : never;

// Even if we directly rewrite Omit to be homomorphic, we still lose assignability with EOPT
type OmitHomomorphicFromWrapped<t> =
  t extends WrappedT<infer inner>
    ? WrappedT<HomomorphicOmit<inner, "bar">>
    : never;

type HomomorphicOmit<t, keyToOmit> = {
  [k in keyof t as k extends keyToOmit ? never : k]: t[k];
};

// Normally in this situation, I have a conform utility to force TS to allow the type
// but this is maybe the first time I've also seen that not work?
type OmitHomomorphicFromWrappedConformed<t> =
  t extends WrappedT<infer inner>
    ? WrappedT<conform<HomomorphicOmit<inner, "bar">, T>>
    : never;

type conform<t, base> = t extends base ? t : base;
🙁 Actual behavior

Applying a homomorphic mapping to a type can break property assignability with EOPT

🙂 Expected behavior

Should preserve assignability

Additional information about the issue

No response

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

Riproduci il comportamento segnalato nel playground TypeScript collegato con exactOptionalPropertyTypes abilitato, utilizzando gli esempi WrappedT, Omit e HomomorphicOmit. Analizza in che modo i tipi mappati omomorfi e l’inferenza condizionale influenzano l’assegnabilità, quindi verifica che i risultati mappati rimangano assegnabili a T senza l’errore segnalato true | undefined.

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.