Conditional Type

Is it possible to have a type which resolves between two types with a conditional whether a field on an object exists or not.
Type A = {a: string, b: string}
Type B = {c: string, b: number}

Type C = A | B
Type A = {a: string, b: string}
Type B = {c: string, b: number}

Type C = A | B
I want to achieve something like this
const obj: C;
obj.a ? obj.b : obj.b + 1
const obj: C;
obj.a ? obj.b : obj.b + 1
I know the example can be resolved with another way but my use case is more complex. I just want the type to be resolved like that. How do you go about this?
2 Replies
Lofty!
Lofty!17mo ago
the only way if extending a type would be like
type Matches<T> = T extends A ? B : T;
type Matches<T> = T extends A ? B : T;
Want results from more Discord servers?
Add your server