mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
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
This commit is contained in:
commit
320db5ccfe
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
},
|
||||
|
||||
@ -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 => {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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')'?
|
||||
|
||||
18
tests/baselines/reference/importUsedAsTypeWithErrors.js
Normal file
18
tests/baselines/reference/importUsedAsTypeWithErrors.js
Normal file
@ -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;
|
||||
12
tests/baselines/reference/importUsedAsTypeWithErrors.symbols
Normal file
12
tests/baselines/reference/importUsedAsTypeWithErrors.symbols
Normal file
@ -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))
|
||||
|
||||
13
tests/baselines/reference/importUsedAsTypeWithErrors.types
Normal file
13
tests/baselines/reference/importUsedAsTypeWithErrors.types
Normal file
@ -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
|
||||
|
||||
7
tests/cases/compiler/importUsedAsTypeWithErrors.ts
Normal file
7
tests/cases/compiler/importUsedAsTypeWithErrors.ts
Normal file
@ -0,0 +1,7 @@
|
||||
// @filename: test.ts
|
||||
export interface T {
|
||||
value: string
|
||||
}
|
||||
|
||||
// @filename: main.ts
|
||||
export const a: import("./test") = null;
|
||||
Loading…
x
Reference in New Issue
Block a user