microsoft / microsoft/TypeScript

Incorrect generic inference inside a discriminated union

Aperta
#45,809 3 commenti 3 reazioni 1 assegnatario Vedi su GitHub

@weswigham ci sta già lavorando.

Dal 10/9/2021.

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

Descrizione

Bug Report

🔎 Search Terms

discrimated union generic extra property

🕗 Version & Regression Information
  • This changed between versions 4.1.5 and 4.2.3
⏯ Playground Link

Playground link with relevant code

💻 Code
type Loadable<TValue> =
    | {
          state: "loading"
      }
    | {
          state: "complete"
          value: TValue
      }


function mapLoadable<TValue>(loadable: Loadable<TValue>, map: (value: TValue) => void): void {
}

type AugmentedLoadable =
    | {
          state: "loading"
          value: number
      }
    | {
          state: "complete"
          value: string
      }

function mapAugmented(augmented: AugmentedLoadable) {
    mapLoadable(augmented, () => {}) // ❌ Errors with "Argument of type 'AugmentedLoadable' is not assignable to parameter of type 'Loadable<number>'"
    mapLoadable<string>(augmented, () => {}) // ✅ Works now that type is generic is specified
}
🙁 Actual behavior

TypeScript incorrectly infers that augmented should be assignable to Loadable<number> rather than a Loadable<string>, because we've added an unrelated value: number property to it. It can be nudged into working by writing out the generic type.

🙂 Expected behavior

TypeScript to infer the correct generic of string.

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.