-
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
TypeScript Version: 3.4.3, typescript@3.5.0-dev.20190518
Search Terms: unsound coercion object boolean
Code
const obj: { toFixed: unknown } = 0;
// val has type "123"
const val = obj && "123";
console.log(val.slice()); // <- runtime error!Expected behavior:
Compile error.
Actual behavior:
This code compiles and causes a runtime error when run.
TypeScript assumes that any value with object type (except {}) is truthy, but it's not true as illustrated above. If one of 0, "", false or 0n is assignable to an object type, it should not be considered truthy.
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed