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:
Mohamed Hegazy 2018-07-05 11:11:35 -07:00 committed by GitHub
commit 320db5ccfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 4 deletions

View File

@ -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;
}

View File

@ -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
},

View File

@ -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 => {

View File

@ -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;

View File

@ -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')'?

View 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;

View 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))

View 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

View File

@ -0,0 +1,7 @@
// @filename: test.ts
export interface T {
value: string
}
// @filename: main.ts
export const a: import("./test") = null;