3 ms·
> A type guard for a dotted name has no effect if any part of the dotted name is the target of an assignment within the guarded code. For example, a type guard
by kbsletten 11y ago
> A type guard for a dotted name has no effect if any part of the dotted name is the target of an assignment within the guarded code. For example, a type guard for x.y.z will have no effect if the guarded code contains any assignments to x, x.y, or x.y.z.
This is interesting to me because it is:
1) Unnecessarily conservative.
let x = { y: { z: intOrNull() } };
if (x && x.y && x.y.z) {
const z = x.y.z;
x.y = null;
// z is of type "number?" when it could be "number"
}
2) Not sound w.r.t. aliasing.
let x = { y: { z: intOrNull() } };
let x_ = x;
if (x && x.y && x.y.z) {
x_.y.z = null;
const z = x.y.z;
// z is of type "number" with a value of "null"
}