Closed
Description
From a discussion with @wycats.
Currently, even if a type is narrowed down to nothing
, we still consider control flow errors in those branches even though you might just be handling error cases:
// Error: Not all code paths return a value.
function foo(x: string | number) {
if (typeof x === "string") {
return true;
}
else if (typeof x === "number") {
return false;
}
fail("Unexhaustive!");
}
function fail(message: string) {
throw message;
}
Similarly, if you add a type annotation, you'll get an error.
// Error: Function lacks ending return statement but return type lacks 'undefined'
function foo(x: string | number): boolean {
if (typeof x === "string") {
return true;
}
else if (typeof x === "number") {
return false;
}
fail("Unexhaustive!");
}
function fail(message: string) {
throw message;
}
Metadata
Metadata
Assignees
Labels
No labels