Fix JSDoc @param qualified name error: produce TS2749 instead of TS2503#3706
Fix JSDoc @param qualified name error: produce TS2749 instead of TS2503#3706
Conversation
…d of TS2503 In JS files, when a JSDoc type reference uses a qualified name (e.g. map.foo) where the left side is a value, tsgo was producing the unhelpful TS2503 "Cannot find namespace 'map'" error. TypeScript produces the more accurate TS2749 "'map.foo' refers to a value, but is being used as a type here". The fix adds two mechanisms: 1. In resolveEntityName, compute namespaceMeaning that includes Value for JS files (matching TypeScript's behavior). 2. In resolveQualifiedName, try resolving the left side with just Namespace first, then fall back to namespaceMeaning (including Value) for JS files. 3. In getTypeFromTypeReference, add a two-pass resolution for JSDoc qualified type references: first try Type-only, then fall back to Type|Value to enable better error reporting. Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/be55bf87-9a50-4982-84e1-d49281ca994d Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
DanielRosenwasser
left a comment
There was a problem hiding this comment.
@copilot maybe there is a misunderstanding. We do not want to go back to understanding obj.foo in type position as typeof obj.foo. But we do want to issue the error message that TypeScript 6 reports;
'obj.foo' refers to a value, but is being used as a type here. Did you mean 'typeof obj.foo'?
|
|
||
| func (c *Checker) hasQualifiedTypeName(node *ast.Node) bool { | ||
| name := getTypeReferenceName(node) | ||
| return name != nil && (name.Kind == ast.KindQualifiedName || name.Kind == ast.KindPropertyAccessExpression) |
There was a problem hiding this comment.
Do we ever actually parse as a property access? Seems like an unnecessary check.
There was a problem hiding this comment.
You're right — JSDoc TypeReference nodes always have a TypeName that's either an Identifier or QualifiedName, never a PropertyAccessExpression. Removed in 40a196a.
…Name JSDoc TypeReference nodes always have a TypeName that is either an Identifier or QualifiedName, never a PropertyAccessExpression. Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/63de4b7d-b3a4-4a74-884c-d1f70828a618 Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Understood — this PR only changes the error message from TS2503 to TS2749. It does not resolve |
In JS files, JSDoc type references using qualified names (e.g.
@param {map.foo}) where the left side is a value produced TS2503 "Cannot find namespace 'map'" instead of the more accurate TS2749 "'map.foo' refers to a value, but is being used as a type here. Did you mean 'typeof map.foo'?"Changes
resolveEntityName: ComputenamespaceMeaning = Namespace | (isInJSFile ? meaning & Value : 0)matching TypeScript's behavior, and pass it toresolveQualifiedNameresolveQualifiedName: Two-step left-side resolution — first tryNamespace-only silently, then fall back tonamespaceMeaning(includingValuefor JS files). If both fail, re-resolve withNamespaceto emit the original errorgetTypeFromTypeReference: For JSDoc qualified type references, add two-pass resolution (firstTypewith ignoreErrors, thenType|Value) to enable thecanSuggestTypeoferror path inresolveQualifiedNameisJSDocTypeReference/hasQualifiedTypeName: Helper methods to scope the two-pass to only qualified names, avoiding interference with simple identifier resolution (which lacksgetTypeFromJSDocValueReferencesupport)Baseline improvements
~20 existing tests improved from unhelpful TS2503 "Cannot find namespace" to accurate TS2749/TS2694 errors. One test (
typeLookupInIIFE) now matches tsc exactly (diff file removed). One test (typeFromPropertyAssignment5) now produces no errors, matching tsc.