I have a simplified version of a more complex problem. The following causes TSC to throw errors:
type Demo<isTrue> = isTrue extends true ? { a: string } : isTrue extends false ? { b: string } : never;
const func = <T extends boolean>(arg: T): Demo<T> => {
if (arg) {
return {a: "hello" };
} else {
return { b: "world" };
}
};
const out = func(true);
Throws the following errors:
Type '{ a: string; }' is not assignable to type 'Demo<T>'.
Type '{ b: string; }' is not assignable to type 'Demo<T>'.
The out
at the bottom has the correct type on inspection, so just the function definition has the issue. How can I understand this better and how do I solve it?