-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
🔎 Search Terms
"assigning never, undefined", "assertion control flow", "never control flow", #32695
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about distributive conditional types, never types
⏯ Playground Link
💻 Code
declare function assert(expr: false, message: string): never;
declare function assert(expr: true, message: string): void;
declare function assert<T>(expr: T, message: string): asserts expr is NonNullable<T>;
interface Foo {
obj?: Object,
}
function pass(foo: Foo) {
if (!foo.obj) {
assert(false, "property not set!");
}
foo.obj.toString();
}
function pass2(foo: Foo) {
assert(foo.obj, "property not set!");
foo.obj.toString();
}
function fail(foo: Foo) {
const x = assert(foo.obj, "property not set");
foo.obj.toString(); // Error: foo.obj may be undefined or null
}🙁 Actual behavior
Compiler is unable to infer that execution aborts with the assert() call if the resulting never is captured/assigned to a variable.
🙂 Expected behavior
(Nullability/never) control flow analysis should flow past the assignment and continue to subsequent code. If assert() returns never, regardless of whether that never is assigned to a variable or not, the execution cannot flow and the code in question cannot be reached.
Additional information about the issue
Forgive me if this is a known issue; I looked at #32695 and the issues mentioning it, but couldn't find an example that I considered to be a narrow enough match for this one instead of an overlapping or adjacent issue.
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed