Microsoft/TypeScript

incorrect inference in union of tuple types

开放

#57,522 创建于 2024年2月24日

 (2 条评论) (0 个反应) (0 位负责人)TypeScript (13,395 个派生)batch import
Domain: check: Type InferenceHelp WantedPossible Improvement

仓库指标

星标
 (108,860 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

🔎 Search Terms

tuple type union, inference, discriminator

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.3#code/C4TwDgpgBAggdgEwDwBUB8UC8UDaAiAQ0TwBooUIAnAW1TTIproF0AoUSKAUQEc6tceCD1Lk2HaAElgAZ1RkA0lAgAPYBEQyoAawggA9gDNyGbPgCWs0QoZVaKHAuZpx4aI3unufdFAA+UNJyKGS6Bsa+AfDI6KyshgCucADGwOb6cFBglBAI5skE6nQAFOo0AFzkdnQAlFAA3qxQzVA5wAmUmWXUrAC+cckZMsBQwJUeSPUE5XAJ1ABGVL1e2bn5hRDFFlZkhKL4wqIADMzMNaxAA

💻 Code

type And<T> = ["and", Term<T>, Term<T>]
type Eq<T> = ["eq", T]
type Its<T, K extends keyof T> = ["its", K, Term<T[K]>]
type Term<T> = Eq<T> | Its<T, keyof T> | And<T>

function predicate<T>(term: Term<T>) {
    return term
}

const t: Term<{a:number}> = predicate(["its", "a", ["eq", 0]])

🙁 Actual behavior

TypeScript complains that Type "a" is not assignable to type number | "toString" | "toFixed" | "toExponential" | "toPrecision" | "valueOf" | "toLocaleString" | Term<number>. The expected types are a union of number from Eq<number>, Term<T> from And<T> and keyof number from Its<number>. Eq<number> can be ruled out by both length and first element and And<T> by the first element. number was chosen as value of T despite there being a solution that is also clear for the expected return type.

🙂 Expected behavior

TypeScript infers T to be { a: number } (or { a: any } when the return type is not known).

Additional information about the issue

It works when you remove And from Term, moving the "and" to the last index or when modelling something similar using objects instead of tuples. The first element could be used to discriminate the union.

贡献者指南