mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-02 07:43:42 +08:00
Node erases a type assertion, so `typeof (stored as R).member` reads the property off whichever value is stored: when `stored` is `R | Record<K, R>` and the record arm is holding, the read answers undefined and the probe is false. Two earlier decisions made this throw instead. lowerAsExpression applied the checked union extraction for `stored as R` eagerly, trapping on exactly the arm the probe exists to detect; and lowerFieldRead answered the read from the ASSERTED checker type, which also folded `typeof` to a constant. Defer the assertion's extraction for a member PRESENCE PROBE - the operand of `typeof`, or an operand of `===`/`!==`/`==`/`!=` against undefined, null or `void 0` - when the stored union's record arms declare the member unevenly. The read then answers per arm off the runtime tag (assertedMemberRead), each declaring arm reading its slot and the rest answering undefined; a read with no per-arm answer still applies the extraction the assertion would have taken. `"member" in (stored as R)` over the same shape likewise probes the stored value. Every real use - the member called, compared by identity, read into a typed slot - keeps the checked extraction, so a lying assertion still throws at its use. Types don't lie; a probe simply isn't the use. Corpus program asserted-arm-probe.ts covers typeof, undefined and nullish probes on the optional and required shapes, `in` on the required shape, a probe guarding the real call, and a real use that throws. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>