Closed
Description
TypeScript Version: 3.8.2, 3.9.2
Search Terms: unreachable code, asserts, assertion, nonnullable, type guard
Expected behavior: The code compiles without warnings.
Actual behavior: Any code after assertNonNull(false)
is marked as unreachable.
Related Issues: -
Code
function assertNonNull<T>(value: T): asserts value is NonNullable<T> {
if (value === null || value === undefined) {
throw new Error(`Expected value to be non-nullable, but got ${JSON.stringify(value)}`);
}
}
assertNonNull(0);
assertNonNull('');
assertNonNull(false);
console.log("end"); // Unreachable code detected.
Output
"use strict";
function assertNonNull(value) {
if (value === null || value === undefined) {
throw new Error(`Expected value to be non-nullable, but got ${JSON.stringify(value)}`);
}
}
assertNonNull(0);
assertNonNull('');
assertNonNull(false);
console.log("end"); // Unreachable code detected.
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "ES2017",
"jsx": "React",
"module": "ESNext"
}
}
Playground Link: Provided
Metadata
Metadata
Assignees
Labels
No labels