-
Notifications
You must be signed in to change notification settings - Fork 13.1k
feat(compiler): improve error message when type alias is used as valu… #62774
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves error messages when type-only symbols (such as type aliases, interfaces, or type parameters) are accidentally used as values. Instead of showing a generic "Cannot find name" error, it now provides a more helpful error message that explicitly states the identifier refers to a type but is being used as a value.
Key Changes
- Added new diagnostic message TS2888 with format: "Cannot find name '{0}'. '{1}' only refers to a type, but is being used as a value here."
- Modified
getCannotFindNameDiagnosticForNamein checker.ts to detect type-only symbols - Added comprehensive test cases covering type aliases, interfaces, and generic types
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/compiler/diagnosticMessages.json | Added new error diagnostic TS2888 for type-used-as-value scenarios |
| src/compiler/checker.ts | Enhanced name resolution error detection to identify type-only symbols |
| tests/cases/compiler/typeAliasUsedAsValue.ts | Comprehensive test coverage for various type-as-value error cases |
| "Cannot find name '{0}'. '{1}' only refers to a type, but is being used as a value here.": { | ||
| "category": "Error", | ||
| "code": 2888 | ||
| }, |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error code 2888 is significantly out of sequence. It's placed between codes 2552 and 2553 in the file, but 2888 is over 300 codes higher. This breaks the sequential ordering convention used throughout the file. The code should likely be 2553 or the next available sequential code in the 2500s range to maintain consistency with the surrounding diagnostic codes.
| const typeSymbol = resolveName(node, node.escapedText, SymbolFlags.Type, /*nameNotFoundMessage*/ undefined, /*isUse*/ false); | ||
| if (typeSymbol && !(typeSymbol.flags & SymbolFlags.Value)) { | ||
| return Diagnostics.Cannot_find_name_0_1_only_refers_to_a_type_but_is_being_used_as_a_value_here; | ||
| } |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diagnostic message requires two parameters ('{0}' for the identifier name and '{1}' for the type symbol name), but the current implementation doesn't provide a way to pass the second parameter. When this diagnostic is used in onFailedToResolveSymbol (line 3282 in checker.ts), it will call error(errorLocation, nameNotFoundMessage, diagnosticName(nameArg)) which only provides one argument. You need to either: (1) change the diagnostic message to only use one parameter like the existing "'{0}' only refers to a type, but is being used as a value here." (code 2693), or (2) modify the error reporting mechanism to pass both the identifier name and the type symbol name as separate arguments. The existing diagnostic 2693 might already serve this purpose.
@microsoft-github-policy-service agree |
|
@microsoft-github-policy-service agree |
feat(compiler): improve error message when type alias is used as value
Fixes issue where users got confusing "Cannot find name" errors when
accidentally using type aliases as values, now provides clear guidance
that the identifier refers to a type.
Closes #7900