-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2828,6 +2828,10 @@ | |
| "category": "Error", | ||
| "code": 2552 | ||
| }, | ||
| "Cannot find name '{0}'. '{1}' only refers to a type, but is being used as a value here.": { | ||
| "category": "Error", | ||
| "code": 2888 | ||
| }, | ||
|
Comment on lines
+2831
to
+2834
|
||
| "Computed values are not permitted in an enum with string valued members.": { | ||
| "category": "Error", | ||
| "code": 2553 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // @filename: mytypes.ts | ||
| export class MyClass { | ||
| public a: number; | ||
| } | ||
|
|
||
| export type MyClassAlias = MyClass; | ||
|
|
||
| // @filename: main.ts | ||
| import {MyClassAlias} from './mytypes'; | ||
|
|
||
| let a: MyClassAlias = new MyClassAlias(); // Error: should show better message | ||
| let b = MyClassAlias; // Error: should show better message | ||
|
|
||
| // Test with local type alias | ||
| type LocalAlias = string; | ||
| let c = LocalAlias; // Error: should show better message | ||
|
|
||
| // Test with interface | ||
| interface MyInterface { | ||
| prop: string; | ||
| } | ||
| let d = MyInterface; // Error: should show better message | ||
|
|
||
| // Test with generic type alias | ||
| type GenericAlias<T> = T[]; | ||
| let e = GenericAlias; // Error: should show better message |
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 callerror(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.