mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-12 02:13:04 +08:00
After `obj.field = bytes[i]` on a `number | undefined` field, TypeScript narrows later reads to number, so the read extracted the number arm unchecked and an out-of-range read printed 0 instead of undefined. Locals declared `T | undefined` had the same problem inside functions (`let x: number | undefined; x = xs[i]; x + 1` gave 1, not NaN). The optional-read analysis now records instance fields declared exactly `T | undefined` that receive a possibly missing value; their narrowed reads keep the stored union, as runtime-optional record fields already do, and the value propagates into parameters and returns like other optional reads. Locals declared that way join the runtime-optional storage tracking, so bare reads keep the union until a guard proves presence. A member read through such a field throws Node's "Cannot read properties of undefined" TypeError.