Closed as not planned
Description
🔎 Search Terms
When a function has return type never
it can be used as a kind of falsy check:
const test = (input: A|undefined): void => {
if (input === undefined) {
neverReturn();
}
// input is never undefined here.
console.log(input.id);
}
This does not work in all cases, as demonstrated on the playground link. Only when the const has an explicit type never
is handled correctly.
🕗 Version & Regression Information
It does not work in the any TypeScript version available at the PlayGround.
⏯ Playground Link
💻 Code
interface A {
id: number;
}
const neverReturn1 = (): never => {
throw new Error('A');
};
const test1 = (input: A|undefined): void => {
if (input === undefined) {
neverReturn1();
}
// Input can still be undefined according to TS.
console.log(input.id);
}
const neverReturn2: () => never = (): never => {
throw new Error('A');
};
const test2 = (input: A|undefined): void => {
if (input === undefined) {
neverReturn2();
}
// Input is determined to be not undefined.
console.log(input.id);
}
🙁 Actual behavior
Error: Object is possibly 'undefined'.
in test1
.
🙂 Expected behavior
No errors, because neverReturn1()
never returns.
Additional information about the issue
No response