From a6401bb3cea713132ca45198502150aead43c536 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 12 Nov 2014 12:03:04 -0800 Subject: [PATCH] Adding destructuring variable declaration error messages --- src/compiler/checker.ts | 18 ++++++++++-------- .../diagnosticInformationMap.generated.ts | 2 ++ src/compiler/diagnosticMessages.json | 8 ++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5061c2f0262..f11bca7c133 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1646,20 +1646,22 @@ module ts { return parentType; } if (pattern.kind === SyntaxKind.ObjectBindingPattern) { - var name = (declaration.propertyName || declaration.name).text; - var type = getTypeOfPropertyOfType(parentType, name) || - isNumericName(name) && getIndexTypeOfType(parentType, IndexKind.Number) || + var name = declaration.propertyName || declaration.name; + var type = getTypeOfPropertyOfType(parentType, name.text) || + isNumericName(name.text) && getIndexTypeOfType(parentType, IndexKind.Number) || getIndexTypeOfType(parentType, IndexKind.String); + if (!type) { + error(name, Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), declarationNameToString(name)); + } } else { var index = indexOf(pattern.elements, declaration); var type = getTypeOfPropertyOfType(parentType, "" + index) || getIndexTypeOfType(parentType, IndexKind.Number); + if (!type) { + error(declaration, Diagnostics.Type_0_has_no_property_1_and_no_numeric_index_signature, typeToString(parentType), "" + index); + } } - if (!type) { - // Error: Type {0} has no property {1} - return unknownType; - } - return type; + return type || unknownType; } function getTypeForVariableDeclaration(declaration: VariableDeclaration | PropertyDeclaration): Type { diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 35d173b4fdf..72a58797791 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -274,6 +274,8 @@ module ts { Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0: { code: 2455, category: DiagnosticCategory.Error, key: "Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}'." }, Type_alias_0_circularly_references_itself: { code: 2456, category: DiagnosticCategory.Error, key: "Type alias '{0}' circularly references itself." }, Type_alias_name_cannot_be_0: { code: 2457, category: DiagnosticCategory.Error, key: "Type alias name cannot be '{0}'" }, + Type_0_has_no_property_1_and_no_string_index_signature: { code: 2458, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}' and no string index signature." }, + Type_0_has_no_property_1_and_no_numeric_index_signature: { code: 2459, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}' and no numeric index signature." }, 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}'." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 97bfba31d0a..7a9deff62eb 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1092,6 +1092,14 @@ "category": "Error", "code": 2457 }, + "Type '{0}' has no property '{1}' and no string index signature.": { + "category": "Error", + "code": 2458 + }, + "Type '{0}' has no property '{1}' and no numeric index signature.": { + "category": "Error", + "code": 2459 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error",