-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Description
π Search Terms
distribution, spread
π Version & Regression Information
This is the behavior in every version I tried from version 4.1.5 upwards to 5.9.3 and in nightly v6.0.0-dev.20251127
β― Playground Link
π» Code
type CrossProduct<Union, Counter extends unknown[]> =
Counter extends [infer Zero, ...infer Rest]
? (Union extends infer Member
? [Member, ...CrossProduct<Union, Rest>]
: never)
: [];
let test1: CrossProduct<number | string, [undefined]>; // [string] | [number]
type Depth1 = CrossProduct<number | string, [undefined]> // [string] | [number]
let test2: (number | string extends infer Union ? (Union extends unknown ? [Union, ...Depth1]: never) : never); // [string, string] | [number, number] | [string, number] | [number, string]
let test3: (number | string extends infer Union ? (Union extends unknown ? [Union, ...CrossProduct<number | string, [undefined]>]: never) : never); // [string, string] | [number, number]
let test4: (number | string extends infer Union ? (Union extends unknown ? [Union, ...([string] | [number])]: never) : never); // [string, string] | [number, number] | [string, number] | [number, string]π Actual behavior
The types of test2 and test4 evaluate to the correct, expected, fully distributed type, while the type of test3 is incompletely distributed (as displayed by IntelliSense in the Playground).
The only difference between test2 and test3 is that in test3 the type that is aliased as Depth1 in test2 is inlined in test3. Thus, both should evaluate to the same type. The type of test2 is correct.
test4 also evaluates correctly. Here the alias is replaced by the literal evaluation result.
π Expected behavior
The type of variable test3 should evaluate to
[string, string] | [number, number] | [string, number] | [number, string]
But it evaluates to
[string, string] | [number, number]
Additional information about the issue
This problem matters since the recursive call in test3 is what I actually need for the construction of a respective recursive type.