function uhoh (x:string|number) {
if (typeof x === 'string') {
return x.length
} else {
return 'bar'
}
}
const a = uhoh('foo') //= string | number
const b = uhoh(3) //= string | number
You may already be aware of this, but it's semi-fixable in TS1:
function uhoh (x:string|number) {
if (typeof x === 'string') {
return x.length
} else {
return 'bar'
}
}
function uhoh (x:string): number;
function uhoh (x:number): string;
const a = uhoh('foo') //= number
const b = uhoh(3) //= string
Sort of. I tried the above and it reported the following error message:
error TS2354: No best common type exists among return expressions.
So at least it detects the problem. However the type inference algorithm should arguably return a union type here.
I find it good practice to always specify the return type of functions anyway. Then it will pick up cases where you return a type you didn't intend to; instead of just using a more general type it will tell you about the inconsistency.
Typescript does not infer union types for return types. You can explicitly annotate them just fine though.
I suppose the reasoning is that, when adding types to js, this is exactly the kind of bad behaviour you want to catch. But I'm not sure, perhaps there are just technical implications for not inferring union return types.