I'm running into an issue when trying to implement a function whose return type is a generic conditional type of its input. For some reason, when I try to implement it, the type checker seems to think that nothing is assignable to Generic<T>, even predefined constant values that are declared as an instance of Generic. I'm pretty sure this is some kind of Typescript limitation, but I'm curious what the limiting factor is here. Is it the conditional type? Or does it have to do with how Generics are dealt with in function implementations?
type A1 = { type: "A", a: false }
type A2 = { type: "A", a: true }
type B1 = { type: "B", b: false }
type B2 = { type: "B", b: true }
type C1 = { type: "C", c: false }
type C2 = { type: "C", c: true }
type First = A1 | B1 | C1
type Second = A2 | B2 | C2
type Letter = First | Second
type GetSecond<T extends Letter> = (
T extends Second ? Second :
T extends A1 ? A2 :
T extends B1 ? B2 :
T extends C1 ? C2 :
never
);
const test: GetSecond<A1> = {
type: "A",
a: true,
}
const func = <T extends Letter>(arg: T): GetSecond<T> => {
switch (arg.type) {
case "A":
if (arg.a) {
return arg
} else {
return test
}
default:
throw new Error()
}
}