The following function was largely lifted from the typescript handbook section on using conditional types, yet it doesn't work:
function test<T extends boolean>(a: T): T extends true ? string : number {
return a ? '1' : 1
}
Typescript is reporting that:
Type '1 | "1"' is not assignable to type 'T extends true ? string : number'.
Type '1' is not assignable to type 'T extends true ? string : number'.
I imagine I'm missing something obvious. How can I construct this function so that typescript correctly infers the type based on the function's argument?
I realize that this specific problem could be solved using function signature overloading, but I'd like to learn more about conditional types.