microsoft / microsoft/TypeScript

Incorrect widening (constraint fallback?) during inference when union with `undefined` appears in constraint

Aperta
#44,891 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

union, type inference, imprecise union generic type, generic type.

🕗 Version & Regression Information

This is reproducible on all versions of the playground.

⏯ Playground Link

Playground link with relevant code

💻 Code
export interface Something<
  R extends
    | {
        [K in 'foo' | 'bar' | 'baz']?: {
          r?: unknown;
          p?: unknown;
        };
      }
    | undefined = undefined
> {}

const o: Something<{ foo: { r: 1; p: 2 }; bar: { r: 3 } }> = {};

declare function something<M extends T extends Something<infer R> ? keyof R : never, T extends Something>(
  key: M,
  something: T
): M;

declare function somethingBugged<M extends T extends Something<infer R> ? keyof R : never, T extends Something>(
  key: M,
  something: T
): [M];

const normal = something('foo', o);
// got: "foo"
// expected: "foo"

const bugged = somethingBugged('foo', o);
// got: ["foo" | "bar"]
// expected: ["foo"]

// However with `as const`, the correct type is returned.
const ugly = somethingBugged('foo' as const, o);
// got: ["foo"]
// expected: ["foo"]
🙁 Actual behavior

The type loses its precision and is the union instead of the exact type.

🙂 Expected behavior

The type is exact.


What is strange is that the generic type M loses precision ("foo" -> "foo" | "bar") with the somethingBugged function which returns a tuple of M (and it does the same thing with any other "wrapper", such as an object).
Whereas with the something function there is no problem. Both functions are identical.

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 collegato di TypeScript Playground e confronta l’inferenza per something e somethingBugged, soprattutto quando il tipo inferito è racchiuso in una tupla o in un oggetto. Analizza perché M si amplia da "foo" a "foo" | "bar" solo nel tipo restituito racchiuso. Il lavoro è completo quando la forma racchiusa conserva il tipo esatto "foo" senza richiedere as const.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.