microsoft / microsoft/TypeScript

Incorrect generic inference inside a discriminated union

Abierto
#45,809 3 comentarios 3 reacciones 1 asignado Ver en GitHub

@weswigham ya está trabajando en esto.

Desde el 10/9/2021.

Bug Domain: check: Type Inference Rescheduled
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.