Skip to content

fix: suppress TS4055/TS4073 for protected methods using typeof parameter#63222

Open
umeshmore45 wants to merge 3 commits intomicrosoft:mainfrom
umeshmore45:fix/protected-method-typeof-parameter-ts4055-v2
Open

fix: suppress TS4055/TS4073 for protected methods using typeof parameter#63222
umeshmore45 wants to merge 3 commits intomicrosoft:mainfrom
umeshmore45:fix/protected-method-typeof-parameter-ts4055-v2

Conversation

@umeshmore45
Copy link

Fixes #61591

Problem

Using typeof parameterName in the return type of a protected method incorrectly triggered TS4055. The same pattern on a public method worked fine.

export class A {
  protected getPropertyValue(
    properties: Properties,
    propertyName: keyof Properties,
  ): Properties[typeof propertyName] { // TS4055: private name 'propertyName'
    return properties[propertyName];
  }
}

Root Cause

During declaration emit, isEntityNameVisible resolves the symbol for typeof propertyName and calls hasVisibleDeclarations. Parameters are not part of any module export scope, so isDeclarationVisible returns false for them, causing a NotAccessible result and triggering TS4055.

Fix

Added an early return in isEntityNameVisible for parameter symbols, mirroring the existing short-circuit for SymbolFlags.TypeParameter. Parameters are always accessible within their function's scope — visibility modifiers on the method don't change that.

if (symbol.declarations && every(symbol.declarations, isParameter)) {
    return { accessibility: SymbolAccessibility.Accessible };
}

Checklist

  • Associated issue in the Backlog milestone
  • Code is up-to-date with main
  • hereby runtests passes locally (46,182 conformance tests)
  • New test case: tests/cases/compiler/protectedMethodTypeofParameter.ts

Copilot AI review requested due to automatic review settings March 8, 2026 12:15
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Mar 8, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an incorrect TS4055/TS4073 accessibility diagnostic during declaration emit when a protected method signature uses a typeof parameterName type query.

Changes:

  • Updates isEntityNameVisible to treat parameter symbols as always accessible within their containing signature during declaration emit.
  • Adds a new compiler test case for protected method return types using typeof on a parameter.
  • Adds new baseline outputs for the new test.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/compiler/checker.ts Short-circuits symbol visibility for parameter symbols to prevent TS4055/TS4073 in declaration emit for typeof paramName.
tests/cases/compiler/protectedMethodTypeofParameter.ts New regression test covering protected method return type with Properties[typeof propertyName].
tests/cases/compiler/protectedMethodTypeofParameter.d.ts Adds a .d.ts alongside the test case (looks like emitted output); flagged in review as likely unintended.
tests/baselines/reference/protectedMethodTypeofParameter.js Baselines the emit output (JS + .d.ts section) for the new test.
tests/baselines/reference/protectedMethodTypeofParameter.types Baselines type information for the .ts input.
tests/baselines/reference/protectedMethodTypeofParameter.symbols Baselines symbol information for the .ts input.
tests/baselines/reference/protectedMethodTypeofParameter.d.types Baselines type information for the emitted .d.ts.
tests/baselines/reference/protectedMethodTypeofParameter.d.symbols Baselines symbol information for the emitted .d.ts.

You can also share your feedback on Copilot code review. Take the survey.

Parameters are always in scope within their enclosing function. When
`typeof paramName` is used in a return type annotation, the parameter
symbol should be considered accessible regardless of method visibility
(public, protected, private), just like type parameters are. Previously,
`isEntityNameVisible` would fall through to `hasVisibleDeclarations`
for parameter symbols, which returned `undefined` (parameters are not
module-level exports), causing a false-positive TS4055/TS4073 error for
protected methods using `typeof parameter` in their return type.

Fixes the case:
  protected method(): Properties[typeof paramName] -- no longer emits TS4055
@umeshmore45 umeshmore45 force-pushed the fix/protected-method-typeof-parameter-ts4055-v2 branch from 2ebfe78 to c000090 Compare March 8, 2026 12:49
@umeshmore45 umeshmore45 requested a review from Copilot March 8, 2026 12:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 9 changed files in this pull request and generated 1 comment.


You can also share your feedback on Copilot code review. Take the survey.

…e queries

Refined the accessibility check for parameter symbols to only apply when they are part of a TypeQuery. This prevents parameter identifiers used as computed property names in type printing from being incorrectly treated as accessible unique-symbol-like keys.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated no new comments.


You can also share your feedback on Copilot code review. Take the survey.

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

Labels

None yet

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

TS4055 when generating declaration for return type that uses type of paramter for protected methods

2 participants