Skip to content

Commit 320db5c

Browse files
authored
Merge pull request #25433 from a-tarasyuk/bug/24839-give-a-better-error-message-for-using-import-as-a-type
24839 - Give a better error message for using import as a type
2 parents 50ef631 + f851ab8 commit 320db5c

9 files changed

+73
-4
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9264,7 +9264,12 @@ namespace ts {
92649264
resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
92659265
}
92669266
else {
9267-
error(node, targetMeaning === SymbolFlags.Value ? Diagnostics.Module_0_does_not_refer_to_a_value_but_is_used_as_a_value_here : Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here, moduleName);
9267+
const errorMessage = targetMeaning === SymbolFlags.Value
9268+
? Diagnostics.Module_0_does_not_refer_to_a_value_but_is_used_as_a_value_here
9269+
: Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0;
9270+
9271+
error(node, errorMessage, moduleName);
9272+
92689273
links.resolvedSymbol = unknownSymbol;
92699274
links.resolvedType = errorType;
92709275
}

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@
971971
"category": "Error",
972972
"code": 1339
973973
},
974-
"Module '{0}' does not refer to a type, but is used as a type here.": {
974+
"Module '{0}' does not refer to a type, but is used as a type here. Did you mean 'typeof import('{0}')'?": {
975975
"category": "Error",
976976
"code": 1340
977977
},

src/services/codefixes/fixAddModuleReferTypeMissingTypeof.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
namespace ts.codefix {
33
const fixIdAddMissingTypeof = "fixAddModuleReferTypeMissingTypeof";
44
const fixId = fixIdAddMissingTypeof;
5-
const errorCodes = [Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here.code];
5+
const errorCodes = [Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0.code];
6+
67
registerCodeFix({
78
errorCodes,
89
getCodeActions: context => {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5073,7 +5073,7 @@ declare namespace ts {
50735073
An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead: DiagnosticMessage;
50745074
infer_declarations_are_only_permitted_in_the_extends_clause_of_a_conditional_type: DiagnosticMessage;
50755075
Module_0_does_not_refer_to_a_value_but_is_used_as_a_value_here: DiagnosticMessage;
5076-
Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here: DiagnosticMessage;
5076+
Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0: DiagnosticMessage;
50775077
Type_arguments_cannot_be_used_here: DiagnosticMessage;
50785078
The_import_meta_meta_property_is_only_allowed_using_ESNext_for_the_target_and_module_compiler_options: DiagnosticMessage;
50795079
Duplicate_identifier_0: DiagnosticMessage;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/main.ts(1,17): error TS1340: Module './test' does not refer to a type, but is used as a type here. Did you mean 'typeof import('./test')'?
2+
3+
4+
==== tests/cases/compiler/test.ts (0 errors) ====
5+
export interface T {
6+
value: string
7+
}
8+
9+
==== tests/cases/compiler/main.ts (1 errors) ====
10+
export const a: import("./test") = null;
11+
~~~~~~~~~~~~~~~~
12+
!!! error TS1340: Module './test' does not refer to a type, but is used as a type here. Did you mean 'typeof import('./test')'?
13+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/importUsedAsTypeWithErrors.ts] ////
2+
3+
//// [test.ts]
4+
export interface T {
5+
value: string
6+
}
7+
8+
//// [main.ts]
9+
export const a: import("./test") = null;
10+
11+
12+
//// [test.js]
13+
"use strict";
14+
exports.__esModule = true;
15+
//// [main.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports.a = null;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/test.ts ===
2+
export interface T {
3+
>T : Symbol(T, Decl(test.ts, 0, 0))
4+
5+
value: string
6+
>value : Symbol(T.value, Decl(test.ts, 0, 20))
7+
}
8+
9+
=== tests/cases/compiler/main.ts ===
10+
export const a: import("./test") = null;
11+
>a : Symbol(a, Decl(main.ts, 0, 12))
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/test.ts ===
2+
export interface T {
3+
>T : T
4+
5+
value: string
6+
>value : string
7+
}
8+
9+
=== tests/cases/compiler/main.ts ===
10+
export const a: import("./test") = null;
11+
>a : any
12+
>null : null
13+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @filename: test.ts
2+
export interface T {
3+
value: string
4+
}
5+
6+
// @filename: main.ts
7+
export const a: import("./test") = null;

0 commit comments

Comments
 (0)