fix(25770): add diagnostic message for the possible mapped type used as an index (#39973)

This commit is contained in:
Alexander T
2020-08-21 20:42:48 +03:00
committed by GitHub
parent 6ec3629ef4
commit 9569198df6
7 changed files with 251 additions and 4 deletions

View File

@@ -2243,16 +2243,32 @@ namespace ts {
}
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) {
const message = isES2015OrLaterConstructorName(name)
? Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later
: Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here;
error(errorLocation, message, unescapeLeadingUnderscores(name));
const rawName = unescapeLeadingUnderscores(name);
if (isES2015OrLaterConstructorName(name)) {
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later, rawName);
}
else if (maybeMappedType(errorLocation, symbol)) {
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Did_you_mean_to_use_1_in_0, rawName, rawName === "K" ? "P" : "K");
}
else {
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, rawName);
}
return true;
}
}
return false;
}
function maybeMappedType(node: Node, symbol: Symbol) {
const container = findAncestor(node.parent, n =>
isComputedPropertyName(n) || isPropertySignature(n) ? false : isTypeLiteralNode(n) || "quit") as TypeLiteralNode | undefined;
if (container && container.members.length === 1) {
const type = getDeclaredTypeOfSymbol(symbol);
return !!(type.flags & TypeFlags.Union) && allTypesAssignableToKind(type, TypeFlags.StringOrNumberLiteral, /*strict*/ true);
}
return false;
}
function isES2015OrLaterConstructorName(n: __String) {
switch (n) {
case "Promise":

View File

@@ -2607,6 +2607,10 @@
"category": "Error",
"code": 2689
},
"'{0}' only refers to a type, but is being used as a value here. Did you mean to use '{1} in {0}'?": {
"category": "Error",
"code": 2690
},
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": {
"category": "Error",
"code": 2691