Fix panic in checker.Type.Types for non-union types via the API (#4426)#4427
Closed
UditDewan wants to merge 2 commits into
Closed
Fix panic in checker.Type.Types for non-union types via the API (#4426)#4427UditDewan wants to merge 2 commits into
UditDewan wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
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.handleGetTypesOfTypeso 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.