Skip to content

Fix JSDoc typeof parameter resolution #62053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sw1tch3roo
Copy link

@sw1tch3roo sw1tch3roo commented Jul 12, 2025

Fixes #61959

Summary

This PR fixes inconsistent typeof lookup/resolution in JSDoc comments where typeof parameterName would produce "Cannot find name" errors instead of correctly resolving to function parameters.

Problem

Previously, when using typeof in JSDoc type annotations like @return {typeof a}, TypeScript would:

  • ❌ Produce error: "Cannot find name 'a'" when no outer variable exists
  • ❌ Incorrectly resolve to outer scope variables when they exist
  • ✅ But go-to-definition and find-all-references would correctly resolve to the parameter

This inconsistency between type checking and navigation features was confusing for users.

Solution

The fix adds JSDoc parameter resolution logic to the resolveNameHelper function in utilities.ts:

  1. Detection: When symbol resolution fails for identifiers in JSDoc typeof contexts
  2. Resolution: Look up the identifier as a function parameter using getHostSignatureFromJSDoc
  3. Consistency: Now typeof a in JSDoc correctly resolves to parameters, matching navigation behavior

Key Changes

  • src/compiler/utilities.ts: Added JSDoc parameter resolution in resolveNameHelper function
  • src/compiler/checker.ts: Enhanced existing JSDoc parameter resolution logic
  • Tests: Added comprehensive test coverage for both type checking and find-all-references

Before/After

Before:

  /**
   * @param {T} a
   * @return {typeof a}  // Error: Cannot find name 'a'
   */
  function f(a) { return a; }

After:

  /**
   * @param {T} a  
   * @return {typeof a}  // Correctly resolves to parameter
   */
  function f(a) { return a; }

Testing

  • ✅ Added jsdocTypeofParameterConsistency.ts compiler test
  • ✅ Added jsdocTypeofParameterResolution.ts fourslash test for find-all-references
  • ✅ All existing tests pass
  • ✅ New baselines show correct type inference: (a: T) => typeof a

This change ensures consistent behavior between type checking and IDE navigation features, resolving the inconsistency reported in #61959.

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Jul 12, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 12, 2025
@sw1tch3roo
Copy link
Author

Hi! @DanielRosenwasser cc

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.

Inconsistent lookup/resolution for typeof in JSDoc in references search
2 participants