diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index db5ded1093e..6bd9a1d9599 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9264,7 +9264,12 @@ namespace ts { resolveImportSymbolType(node, links, moduleSymbol, targetMeaning); } else { - 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); + const errorMessage = 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_Did_you_mean_typeof_import_0; + + error(node, errorMessage, moduleName); + links.resolvedSymbol = unknownSymbol; links.resolvedType = errorType; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 40a4c4d09d4..87d8297e74d 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -971,7 +971,7 @@ "category": "Error", "code": 1339 }, - "Module '{0}' does not refer to a type, but is used as a type here.": { + "Module '{0}' does not refer to a type, but is used as a type here. Did you mean 'typeof import('{0}')'?": { "category": "Error", "code": 1340 }, diff --git a/src/services/codefixes/fixAddModuleReferTypeMissingTypeof.ts b/src/services/codefixes/fixAddModuleReferTypeMissingTypeof.ts index b558a270dec..b382b9a2f05 100644 --- a/src/services/codefixes/fixAddModuleReferTypeMissingTypeof.ts +++ b/src/services/codefixes/fixAddModuleReferTypeMissingTypeof.ts @@ -2,7 +2,8 @@ namespace ts.codefix { const fixIdAddMissingTypeof = "fixAddModuleReferTypeMissingTypeof"; const fixId = fixIdAddMissingTypeof; - const errorCodes = [Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here.code]; + 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]; + registerCodeFix({ errorCodes, getCodeActions: context => { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 99452b93d47..8e3194eef1f 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -5073,7 +5073,7 @@ declare namespace ts { An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead: DiagnosticMessage; infer_declarations_are_only_permitted_in_the_extends_clause_of_a_conditional_type: DiagnosticMessage; Module_0_does_not_refer_to_a_value_but_is_used_as_a_value_here: DiagnosticMessage; - Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here: DiagnosticMessage; + Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0: DiagnosticMessage; Type_arguments_cannot_be_used_here: DiagnosticMessage; The_import_meta_meta_property_is_only_allowed_using_ESNext_for_the_target_and_module_compiler_options: DiagnosticMessage; Duplicate_identifier_0: DiagnosticMessage; diff --git a/tests/baselines/reference/importUsedAsTypeWithErrors.errors.txt b/tests/baselines/reference/importUsedAsTypeWithErrors.errors.txt new file mode 100644 index 00000000000..7d86ce7624a --- /dev/null +++ b/tests/baselines/reference/importUsedAsTypeWithErrors.errors.txt @@ -0,0 +1,13 @@ +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')'? + + +==== tests/cases/compiler/test.ts (0 errors) ==== + export interface T { + value: string + } + +==== tests/cases/compiler/main.ts (1 errors) ==== + export const a: import("./test") = null; + ~~~~~~~~~~~~~~~~ +!!! error TS1340: Module './test' does not refer to a type, but is used as a type here. Did you mean 'typeof import('./test')'? + \ No newline at end of file diff --git a/tests/baselines/reference/importUsedAsTypeWithErrors.js b/tests/baselines/reference/importUsedAsTypeWithErrors.js new file mode 100644 index 00000000000..add7e6b2852 --- /dev/null +++ b/tests/baselines/reference/importUsedAsTypeWithErrors.js @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/importUsedAsTypeWithErrors.ts] //// + +//// [test.ts] +export interface T { + value: string +} + +//// [main.ts] +export const a: import("./test") = null; + + +//// [test.js] +"use strict"; +exports.__esModule = true; +//// [main.js] +"use strict"; +exports.__esModule = true; +exports.a = null; diff --git a/tests/baselines/reference/importUsedAsTypeWithErrors.symbols b/tests/baselines/reference/importUsedAsTypeWithErrors.symbols new file mode 100644 index 00000000000..b6d4017ea6c --- /dev/null +++ b/tests/baselines/reference/importUsedAsTypeWithErrors.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/test.ts === +export interface T { +>T : Symbol(T, Decl(test.ts, 0, 0)) + + value: string +>value : Symbol(T.value, Decl(test.ts, 0, 20)) +} + +=== tests/cases/compiler/main.ts === +export const a: import("./test") = null; +>a : Symbol(a, Decl(main.ts, 0, 12)) + diff --git a/tests/baselines/reference/importUsedAsTypeWithErrors.types b/tests/baselines/reference/importUsedAsTypeWithErrors.types new file mode 100644 index 00000000000..a41de4f658e --- /dev/null +++ b/tests/baselines/reference/importUsedAsTypeWithErrors.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/test.ts === +export interface T { +>T : T + + value: string +>value : string +} + +=== tests/cases/compiler/main.ts === +export const a: import("./test") = null; +>a : any +>null : null + diff --git a/tests/cases/compiler/importUsedAsTypeWithErrors.ts b/tests/cases/compiler/importUsedAsTypeWithErrors.ts new file mode 100644 index 00000000000..36df311bbfe --- /dev/null +++ b/tests/cases/compiler/importUsedAsTypeWithErrors.ts @@ -0,0 +1,7 @@ +// @filename: test.ts +export interface T { + value: string +} + +// @filename: main.ts +export const a: import("./test") = null;