Skip to content

Fix panic in checker.GetTypeArguments for non-reference types (#4338)#4416

Open
UditDewan wants to merge 2 commits into
microsoft:mainfrom
UditDewan:fix/4338-gettypearguments-panic
Open

Fix panic in checker.GetTypeArguments for non-reference types (#4338)#4416
UditDewan wants to merge 2 commits into
microsoft:mainfrom
UditDewan:fix/4338-gettypearguments-panic

Conversation

@UditDewan

Copy link
Copy Markdown

Fixes #4338

Problem

The public API method Checker.GetTypeArguments forwarded straight to the internal getTypeArguments, which assumes its argument is a type reference and immediately does t.AsTypeReference(). For a non-reference type (e.g. the intrinsic string, produced by type Alias = string) that cast returns nil, so the next field access (d.resolvedTypeArguments) is a nil pointer dereference and the server panics:

panic: runtime error: invalid memory address or nil pointer dereference
  checker.(*Checker).getTypeArguments  internal/checker/checker.go:21794
  checker.(*Checker).GetTypeArguments  internal/checker/exports.go:278
  api.(*Session).handleGetTypeArguments internal/api/session.go

The native-preview API types this method as getTypeArguments(type: Type): readonly Type[], i.e. it accepts any Type, so callers can legitimately reach this path with a non-reference type.

Fix

Guard the exported GetTypeArguments so non-reference types return no type arguments instead of panicking. This matches tsc's behavior (non-references have no type arguments) and the readonly Type[] contract. The check uses ObjectFlagsReference — the same gate internal callers already apply before calling getTypeArguments — so the internal hot path is untouched.

func (c *Checker) GetTypeArguments(t *Type) []*Type {
	if t.objectFlags&ObjectFlagsReference == 0 {
		return nil
	}
	return c.getTypeArguments(t)
}

This mirrors the approach taken for the sibling crash in #4335 / PR #4345 (getBaseTypes on non-matching types returns nil).

Tests

Added regression tests to both the sync and async API suites (Checker - getTypeArguments) that call getTypeArguments on the intrinsic string type and assert it returns []. Verified that:

  • Without the fix, the new test reproduces the exact nil-pointer panic from the issue.
  • With the fix, both new tests and the existing getTypeArguments test pass.

gofmt and dprint check are clean.


Claude (Opus 4.8) helped debug this issue.

…oft#4338)

The public API `Checker.GetTypeArguments` forwarded straight to the
internal `getTypeArguments`, which assumes a type reference and
immediately dereferences `t.AsTypeReference()`. Passing a non-reference
type (e.g. the intrinsic `string`) yielded a nil `*TypeReference` and
crashed with a nil pointer dereference.

Guard the exported method so non-reference types return no type
arguments, matching tsc and the native-preview `getTypeArguments(type):
readonly Type[]` contract. Internal callers already gate on
`ObjectFlagsReference` before calling `getTypeArguments`, so the hot
path is unaffected.

Adds sync and async API regression tests that reproduce the original
panic.

Claude (Opus 4.8) helped debug this issue.
Copilot AI review requested due to automatic review settings June 23, 2026 19:49

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

Fixes a server panic in the exported checker API by making Checker.GetTypeArguments safely handle non-reference types (e.g. intrinsic string) and return “no type arguments” instead of dereferencing a nil TypeReference. This aligns the native-preview API’s getTypeArguments(type: Type): readonly Type[] contract with tsc behavior.

Changes:

  • Add an ObjectFlagsReference guard in Checker.GetTypeArguments to avoid nil-deref for non-reference types.
  • Add sync + async native-preview regression tests asserting getTypeArguments(stringType) returns [] (issue #4338).

Reviewed changes

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

File Description
internal/checker/exports.go Adds an exported-API guard so non-reference Types return nil/empty instead of panicking.
_packages/native-preview/test/sync/api.test.ts Adds sync regression test for getTypeArguments on string.
_packages/native-preview/test/async/api.test.ts Adds async regression test for getTypeArguments on string.

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.

checker.getTypeArguments panics for non-reference types

2 participants