You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
functionassertNonNull<T>(value: T): asserts value is NonNullable<T>{if(value===null||value===undefined){thrownewError(`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";functionassertNonNull(value){if(value===null||value===undefined){thrownewError(`Expected value to be non-nullable, but got ${JSON.stringify(value)}`);}}assertNonNull(0);assertNonNull('');assertNonNull(false);console.log("end");// Unreachable code detected.
tsc reports a hard error TS7027: Unreachable code detected. for allowUnreachableCode: false. If the config option is undefined or true, tsc compiles the code without errors. This is expected per tsconfig#allowUnreachableCode, but still does not explain the unexpected behavior of my issue.
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
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: