Apologies if this question seems basic to those well-versed in typesystems. I'm puzzled by the difference in outcomes when inlining a conditional statement in TypeScript.
For instance, using the Extract conditional leads to different results depending on whether it's inlined or used as a generic:
// Initial setup
type TUnion = ['foo', 1] | ['baz', 2];
type TDiscriminator = ['foo', any];
// Using conditional generic
type Extract<T, U> = T extends U ? T : never;
type TExtracted = Extract<TUnion, TDiscriminator>; // ["foo", 1]
// Inlining the conditional
type TInlined = TUnion extends TDiscriminator ? TUnion : never; // never
Upon further exploration, it appears that passing the value of T is necessary:
type ExtractWithU<T> = T extends TDiscriminator ? T : never;
type TExtractedWithU = ExtractWithU<TUnion>; // ["foo", 1]
type ExtractWithT<U> = TUnion extends U ? TUnion : never;
type TExtractedWithT = ExtractWithT<TDiscriminator>; // never
Could I be overlooking something quite simple here?