Skip to content

fix: error on instantiation expressions that end an optional chain - #83

Open
bardrop wants to merge 1 commit into
bloomberg:mainfrom
bardrop-oss:fix-instantiation-ends-optional-chain
Open

bardrop wants to merge 1 commit into
bloomberg:mainfrom
bardrop-oss:fix-instantiation-ends-optional-chain

Conversation

@bardrop

@bardrop bardrop commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #82

TypeScript ends an optional chain at an instantiation expression, so a?.b<T>.c is emitted as (a?.b).c. Only blanking <T> gave a?.b .c, which keeps the chain going (returns undefined instead of throwing when a is nullish), and a?.b<T>.c = 1 / a?.b<T>.c `` became invalid JS.

The ( can't be inserted without moving offsets, so this follows the as / satisfies grouping cases: onError is called and the code is left unchanged when a non-optional .x, [x], (x) or tagged template follows an instantiation expression whose expression is an optional chain (also through !, as in a?.b!<T>.c).

The check is done in the parent (property/element access, call, tagged template) because the visitor runs without parent pointers.

Still erased, as the meaning stays the same: a?.b<T>, a?.b<T>?.c, (a?.b<T>).c, a.b<T>.c, class C extends a?.b<T> {}.

Testing:

  • New tests in tests/errors.test.ts: the error cases fail on main (no onError call) and pass now; the "still erased" cases pass on both.
  • npm test (45 pass), npm run fixtures (no output changes), npm run check, npm run format:check.
  • Ran the old and new build over ~20k local .ts files: identical output and error counts.
  • Added a short section to docs/unsupported_syntax.md.

TypeScript ends an optional chain at an instantiation expression, so
`a?.b<T>.c` is emitted as `(a?.b).c`. Erasing only `<T>` gave
`a?.b   .c`, which keeps the chain going: it returns undefined instead of
throwing when `a` is nullish, and `a?.b<T>.c = 1` became invalid JS.

Like the `as`/`satisfies` grouping cases, call onError and leave the
code unchanged when a non-optional `.x`, `[x]`, `(x)` or template
follows such an instantiation expression.

Signed-off-by: Mradul Singh <msingh@dealmeridian.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instantiation expression that ends an optional chain is erased without parentheses (a?.b<T>.c)

1 participant