Closed
Description
TypeScript Version: up to latest version 3.8-Beta
Search Terms:
- does not exist on type 'never'
- control flow never
- null check never
- type guard null never
Code
let greeting: null | string = null;
/* A error */ function setIt() { greeting = "Hello!" }; setIt()
/* B error */ setTimeout(() => greeting = "Hello!", 0);
/* C error */ const runCallback = (cb: () => void) => cb(); runCallback(() => greeting = "Hello!");
/* D ok */ // ((() => { greeting = "Hello!" })())
/* E ok */ // greeting = Math.random() > 0.5 ? "Hello!" : null;
if (greeting) {
greeting.toUpperCase(); // Property 'toUpperCase' does not exist on type 'never'.
}
if (greeting != null && greeting !== null) {
greeting.toUpperCase(); // Property 'toUpperCase' does not exist on type 'never'.
}
if (typeof greeting === "string") {
greeting.toUpperCase(); // Property 'toUpperCase' does not exist on type 'never'.
}
Expected behavior:
greeting
should have type string
Inside all if-blocks - not never
. This should be true for
all (activated exclusively) test cases (A to E).
Actual behavior:
greeting
has type never
inside the if-blocks (for test cases A, B and C). It appears that the assignments to greeting
are not recognized. For test cases D and E everything is working as expected.
Related Issues: Maybe to #36777