Skip to content

Fix panic during declaration emit with stripInternal on parenthesized and wrapped types - #65624

Open
Shakil Ur Rehman (ShakilUrRehman21) wants to merge 2 commits into
microsoft:mainfrom
ShakilUrRehman21:fix-strip-internal-parenthesized-type-panic
Open

Shakil Ur Rehman (ShakilUrRehman21) wants to merge 2 commits into
microsoft:mainfrom
ShakilUrRehman21:fix-strip-internal-parenthesized-type-panic

Conversation

@ShakilUrRehman21

Copy link
Copy Markdown

Fixes #65623

Analysis

When --declaration and --stripInternal are enabled, encountering an @internal type wrapped in parentheses (e.g. export type T = ( /** @internal */ Foo ); or export const x: ( /** @internal */ Foo ) = ...) causes the compiler to panic with a nil pointer dereference:

panic: runtime error: invalid memory address or nil pointer dereference
github.com/microsoft/TypeScript/tsc/internal/ast.GetTypeNodePrecedence(...)
github.com/microsoft/TypeScript/tsc/internal/printer.(*Printer).emitParenthesizedType(...)

In tsc/internal/transformers/declarations/transform.go, KindParenthesizedType, KindTypeOperator, and KindArrayType fell into the default visitor: result = tx.Visitor().VisitEachChild(input).
When the inner type was elided by --stripInternal (returning nil), VisitEachChild called UpdateParenthesizedTypeNode(input, nil), constructing a node whose inner .Type pointer was nil.

  1. In transformTypeAliasDeclaration, t := tx.Visitor().Visit(input.Type) returned this non-nil ParenthesizedTypeNode, bypassing if t == nil { t = any }.
  2. The printer then called ast.GetTypeNodePrecedence(node.Type) on the inner nil node, dereferencing nil.Kind and panicking.
  3. Similarly, in ensureType for variable declarations, returning a parenthesized type with an inner nil pointer crashed emitTypeAnnotation.

Fix

  1. Added dedicated transform handlers for KindParenthesizedType, KindTypeOperator, and KindArrayType in DeclarationTransformer:
    • If visiting the inner type yields nil (elided by shouldStripInternal), the wrapper returns nil as well.
  2. Returning nil allows transformTypeAliasDeclaration to correctly substitute any (consistent with bare @internal type aliases) and allows ensureType to elide the type annotation cleanly.
  3. Added baseline test declarationEmitStripInternalParenthesizedType.ts covering parenthesized type aliases, variable type annotations, and nested type operators/arrays.

Copilot AI balanced review requested due to automatic review settings November 21, 2026 07:39
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 21, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 21, 2026

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.

🟡 Changes recommended

Declaration emit still panics with --stripInternal when an indexed-access type loses its object operand, requiring nil-aware handling and a regression test.

1 open issue
What changed in this PR

This PR addresses #65623 by preventing declaration-emission panics when --stripInternal removes types inside parentheses, type operators, or arrays.

Changes:

  • Adds wrapper-specific transforms that propagate removal of an inner type.
  • Adds regression cases for type aliases, variable annotations, and nested wrappers.
  • Records expected declaration output and type/symbol baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitStripInternalParenthesizedType.ts Adds regression cases for stripped wrapped types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitStripInternalParenthesizedType.types Records inferred types for the test.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitStripInternalParenthesizedType.symbols Records symbol bindings for the test.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitStripInternalParenthesizedType.js Records JavaScript, declarations, and declaration diagnostics.
tsc/​internal/​transformers/​declarations/​transform.go Adds nil-aware transforms for parentheses, operators, and arrays.

Review effort: Balanced

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

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Panic during declaration emit with stripInternal on parenthesized or wrapped internal type

2 participants