Why does the function foo report an error Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'. in the return value; line and the function bar works as expected?
function foo(value: string | number | null): string | null {
if (typeof value !== 'string' && value !== null) {
throw new Error();
}
return value;
}
function bar(value: string | number): string {
if (typeof value !== 'string') {
throw new Error();
}
return value;
}