mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 07:29:16 -05:00
Specify error message for type argument inference failing
This commit is contained in:
@@ -129,6 +129,7 @@ module ts {
|
||||
var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
|
||||
var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
|
||||
var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
|
||||
var typeArgumentInferenceFailureType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
|
||||
|
||||
var anySignature = createSignature(undefined, undefined, emptyArray, anyType, 0, false, false);
|
||||
var unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, 0, false, false);
|
||||
@@ -3993,23 +3994,26 @@ module ts {
|
||||
}
|
||||
|
||||
function getInferredType(context: InferenceContext, index: number): Type {
|
||||
var result = context.inferredTypes[index];
|
||||
if (!result) {
|
||||
var inferredType = context.inferredTypes[index];
|
||||
if (!inferredType) {
|
||||
var inferences = context.inferences[index];
|
||||
if (inferences.length) {
|
||||
// Infer widened union or supertype, or the undefined type for no common supertype
|
||||
var unionOrSuperType = context.inferUnionTypes ? getUnionType(inferences) : getCommonSupertype(inferences);
|
||||
var inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : undefinedType;
|
||||
inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : typeArgumentInferenceFailureType;
|
||||
}
|
||||
else {
|
||||
// Infer the empty object type when no inferences were made
|
||||
inferredType = emptyObjectType;
|
||||
}
|
||||
var constraint = getConstraintOfTypeParameter(context.typeParameters[index]);
|
||||
var result = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType;
|
||||
context.inferredTypes[index] = result;
|
||||
|
||||
if (inferredType !== typeArgumentInferenceFailureType) {
|
||||
var constraint = getConstraintOfTypeParameter(context.typeParameters[index]);
|
||||
inferredType = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType;
|
||||
}
|
||||
context.inferredTypes[index] = inferredType;
|
||||
}
|
||||
return result;
|
||||
return inferredType;
|
||||
}
|
||||
|
||||
function getInferredTypes(context: InferenceContext): Type[] {
|
||||
@@ -5094,7 +5098,7 @@ module ts {
|
||||
}
|
||||
var inferredTypes = getInferredTypes(context);
|
||||
// Inference has failed if the undefined type is in list of inferences
|
||||
return !contains(inferredTypes, undefinedType);
|
||||
return !contains(inferredTypes, typeArgumentInferenceFailureType);
|
||||
}
|
||||
|
||||
function checkTypeArguments(signature: Signature, typeArguments: TypeNode[], typeArgumentResultTypes: Type[], reportErrors: boolean): boolean {
|
||||
@@ -5161,6 +5165,7 @@ module ts {
|
||||
|
||||
var candidateForArgumentError: Signature;
|
||||
var candidateForTypeArgumentError: Signature;
|
||||
var indexOfUninferredTypeParameter: number;
|
||||
var result: Signature;
|
||||
if (candidates.length > 1) {
|
||||
result = chooseOverload(candidates, subtypeRelation, excludeArgument);
|
||||
@@ -5169,6 +5174,7 @@ module ts {
|
||||
// Reinitialize these pointers for round two
|
||||
candidateForArgumentError = undefined;
|
||||
candidateForTypeArgumentError = undefined;
|
||||
indexOfUninferredTypeParameter = undefined;
|
||||
result = chooseOverload(candidates, assignableRelation, excludeArgument);
|
||||
}
|
||||
if (result) {
|
||||
@@ -5187,7 +5193,8 @@ module ts {
|
||||
checkTypeArguments(candidateForTypeArgumentError, node.typeArguments, [], /*reportErrors*/ true)
|
||||
}
|
||||
else {
|
||||
error(node.func, Diagnostics.The_type_arguments_cannot_be_inferred_from_the_usage_Try_specifying_the_type_arguments_explicitly);
|
||||
error(node.func, Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Try_specifying_the_type_arguments_explicitly,
|
||||
typeToString(candidateForTypeArgumentError.typeParameters[indexOfUninferredTypeParameter]));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -5251,6 +5258,9 @@ module ts {
|
||||
}
|
||||
else {
|
||||
candidateForTypeArgumentError = originalCandidate;
|
||||
if (!node.typeArguments) {
|
||||
indexOfUninferredTypeParameter = typeArgumentTypes.indexOf(typeArgumentInferenceFailureType);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -261,7 +261,7 @@ module ts {
|
||||
Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses: { code: 2445, category: DiagnosticCategory.Error, key: "Property '{0}' is protected and only accessible within class '{1}' and its subclasses." },
|
||||
Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1: { code: 2446, category: DiagnosticCategory.Error, key: "Property '{0}' is protected and only accessible through an instance of class '{1}'." },
|
||||
The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead: { code: 2447, category: DiagnosticCategory.Error, key: "The '{0}' operator is not allowed for boolean types. Consider using '{1}' instead." },
|
||||
The_type_arguments_cannot_be_inferred_from_the_usage_Try_specifying_the_type_arguments_explicitly: { code: 2448, category: DiagnosticCategory.Error, key: "The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly." },
|
||||
The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Try_specifying_the_type_arguments_explicitly: { code: 2448, category: DiagnosticCategory.Error, key: "The type argument for type parameter '{0}' cannot be inferred from the usage. Try specifying the type arguments explicitly." },
|
||||
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
|
||||
Type_parameter_0_of_exported_class_has_or_is_using_name_1_from_private_module_2: { code: 4001, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using name '{1}' from private module '{2}'." },
|
||||
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
|
||||
|
||||
@@ -1036,7 +1036,7 @@
|
||||
"category": "Error",
|
||||
"code": 2447
|
||||
},
|
||||
"The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly.": {
|
||||
"The type argument for type parameter '{0}' cannot be inferred from the usage. Try specifying the type arguments explicitly.": {
|
||||
"category": "Error",
|
||||
"code": 2448
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user