Closed
Description
Bug Report
π Search Terms
conditional type, extends, infer, spread, tuple
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about extends
β― Playground Link
Playground link with relevant code
π» Code
type Foo<K, T extends readonly unknown[]> = T extends readonly [any, ...infer X extends readonly unknown[]] ? X : never;
type Bar<K, T extends readonly unknown[]> = T extends readonly [any, ...infer X extends readonly K[]] ? X : never;
type Baz<K, T extends readonly unknown[]> = T extends readonly [any, ...infer X extends readonly (K | "a" | "b")[]] ? X : never;
type x = Foo<"a" | "b", ["a", "b", "b"]>
// ^? - type x = ["b", "b"]
type y = Bar<"a" | "b", ["a", "b", "b"]>
// ^? - type y = readonly ("a" | "b")[]
type z = Baz<"a" | "b", ["a", "b", "b"]>
// ^? - type z = ["b", "b"]
π Actual behavior
The conditional types do not produce the same output:
type x = Foo<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
type y = Bar<"a" | "b", ["a", "b", "b"]> // readonly ("a" | "b")[]
type z = Baz<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
π Expected behavior
The conditional types should all produce the same output. The fact that K
is a type parameter should not affect the output.
type x = Foo<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
type y = Bar<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
type z = Baz<"a" | "b", ["a", "b", "b"]> // ["b", "b"]
Alternatively if that is infeasible for technical reasons, the conditional type should fail. Inferring the constraint makes traversing the tuple impossible.
Activity