Skip to content

Fix panic in checker.Type.Types for non-union types via the API (#4426)#4427

Closed
UditDewan wants to merge 2 commits into
microsoft:mainfrom
UditDewan:fix/gettypes-panic
Closed

Fix panic in checker.Type.Types for non-union types via the API (#4426)#4427
UditDewan wants to merge 2 commits into
microsoft:mainfrom
UditDewan:fix/gettypes-panic

Conversation

@UditDewan

Copy link
Copy Markdown

Fixes #4426.

Problem — The public getter getTypes() (Type.getTypes() → Session.handleGetTypesOfType → checker.Type.Types) forwards an arbitrary
user type to Type.Types, an invariant accessor that only handles union/intersection and template-literal types and panic("Unhandled
case in Type.Types")s on everything else. Calling getTypes() on e.g. the intrinsic string crashes the request. Unlike the
getTypeParameters/getCheckType family (defended JS-side by ?? [] defaults and handle gates), getTypes() always reaches the server,
so the panic is reachable directly through type.getTypes(). Same class as #4338 (getTypeArguments) and #4335/#4425 (getBaseTypes).

Fix — Guard at the API boundary (internal/api/session.go); non-union types return []. The Type.Types accessor stays strict (meant
to panic on misuse).

Tests — New Type - getTypes block (sync + async): union returns 2 constituents; non-union returns [] (regression for #4426).

Test plan — go build ✓, gofmt clean ✓, 4/4 new tests pass (non-union panics on main, passes here) ✓.

Claude (Opus 4.8) helped investigate, reproduce, and test this fix.

Copilot AI review requested due to automatic review settings June 24, 2026 05:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents a server-side panic when the public native-preview API Type.getTypes() is called on non-union/non-intersection/non-template-literal types (e.g. string), by adding an explicit guard at the API boundary in internal/api/session.go. It keeps checker.Type.Types() strict (panic on misuse) while making the public API resilient.

Changes:

  • Add a flag-based guard in Session.handleGetTypesOfType so unsupported types return no constituents instead of panicking.
  • Add sync native-preview tests verifying getTypes() returns two constituents for a union type.
  • Add sync + async native-preview regression tests verifying getTypes() returns [] (and does not panic) for a non-union type.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
internal/api/session.go Guard getTypesOfType so only union/intersection/template-literal types call Type.Types(); otherwise return an empty result.
_packages/native-preview/test/sync/api.test.ts Add sync regression coverage for Type.getTypes() on union and non-union types.
_packages/native-preview/test/async/api.test.ts Add async regression coverage for Type.getTypes() on union and non-union types.

@UditDewan UditDewan closed this Jun 26, 2026
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.

Panic (Unhandled case in Type.Types) in checker.Type.Types for non-union types (e.g. string) via the API getTypes()

2 participants