mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-02 07:43:42 +08:00
TypeScript infers a function's return type as the subtype-reduced union of
the values it returns: returning `{ status }` in one branch and
`{ status, value }` (or a call returning either) in another infers just
`{ status }`. Records have fixed layouts, so that type cannot hold the
second value: the return failed its union wrap at run time ('... is not
representable in the target union'), and values that reached a binding of
the reduced type failed a checked narrowing.
- A function with an inferred return type and no contextual signature
returns the union of the record types it actually returns, including
through calls of other such functions. Only value types the checker's
reduction dropped from the inferred union are added, so functions whose
inferred type already names every returned type keep their layout.
Declared return types keep their meaning.
- Reading such a union through the reduced type (a binding, a member read,
JSON.stringify) uses a dedicated checked extraction that converts the wider
record arms with the ordinary width coercion; other narrowings are
unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>