mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-31 07:23:06 +08:00
An assignment narrows a binding to the assigned arm, and TypeScript keeps that narrowing across a closure or method that stores another arm. After `if (acc !== null) return acc;` the checker still types the read as the old arm, the return slot rejects the declared `T | null`, and the checked extraction threw on the value the closure stored, where Node returns it. The compiler itself has this shape (lowerDynObjectLiteral), so the native compiler aborted with "a value of another type is not representable in the target union" and closure-nullable-union-return.ts diverged. A slot that accepts the declared type minus null/undefined now reads the stale reference as NonNullable<declared>, like a binding narrowed only by the absence test. Elements and properties of array and object literals are such slots when the literal itself sits in a typed slot (annotated declaration, argument, assignment, return of a function with a declared return type). Untyped literals keep the checked extraction.