I have a method where the results type depends on the argument type. Using the ts type condition still results to some errors. what would be the correct code? and why is this wrong?
function foo<T>(v: T): T extends string ? number : boolean {
if (typeof v === "string") return 12; // TS Error: Type '12' is not assignable to type 'T extends string ? number : boolean'.
return true; // TS Error
}
const b = foo("bar"); // Ok
code in typescript playground