Common fields of union not resolved for generic conditional with `any` branch
#62,466 opened on 2025幎9æ18æ¥
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
"conditional generics with any branch in union", "union with generic conditional branch"
ð Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional generics used in unions but didn't find anything that seemed relevant.
⯠Playground Link
ð» Code
type InCommon = {common: string};
type Conditional<K extends string | number> = K extends number ? {one: number} : Record<string, unknown>;
type UnionWithRegularConditional<K extends string | number> = InCommon | (Conditional<K> & InCommon);
function exampleUnionWithRegular<K extends string | number>(key: K) {
const unionWithRegularConditional = {} as UnionWithRegularConditional<K>;
// this works as expected
unionWithRegularConditional.common;
}
type ConditionalWithAny<K extends string | number> = K extends number ? any : Record<string, unknown>;
type UnionWithAnyConditional<K extends string | number> = InCommon | (ConditionalWithAny<K> & InCommon);
function exampleUnionWithAny<K extends string | number>(key: K) {
const unionWithAnyConditional = {} as UnionWithAnyConditional<K>;
// this has a type error for `common` not existing on `ConditionalWithAny<K> & InCommon`
unionWithAnyConditional.common;
// but then this *does* work
const propWithAnyConditional = {} as (ConditionalWithAny<K> & InCommon);
propWithAnyConditional.common;
}
ð Actual behavior
When a union type contains a conditional generic whose branch is any, the common fields of the union are not being correctly recognized.
This is illustrated above when TS is unable to recognize that common is a field on unionWithAnyConditional.
ð Expected behavior
The expected behavior here is that common fields of the union should be correctly resolved.
From the above example, the expectation is that unionWithAnyConditional.common would not have a "does not exist on type" error.
Additional information about the issue
No response