From 2ed5f418a1715677e567940462312d0406239f63 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 20 Nov 2014 13:45:55 -0800 Subject: [PATCH] Destructuring of tuple type cannot specify extra variables --- src/compiler/checker.ts | 24 +++++++++++++++---- .../diagnosticInformationMap.generated.ts | 3 ++- src/compiler/diagnosticMessages.json | 6 ++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b1a0b4ebd1a..a188e720005 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1644,10 +1644,18 @@ module ts { } } 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 (getPropertyOfType(parentType, "0")) { + var propName = "" + indexOf(pattern.elements, declaration); + var type = getTypeOfPropertyOfType(parentType, propName); + if (!type) { + error(declaration, Diagnostics.Type_0_has_no_property_1, typeToString(parentType), propName); + } + } + else { + var type = getIndexTypeOfType(parentType, IndexKind.Number); + if (!type) { + error(declaration, Diagnostics.Type_0_has_no_numeric_index_signature, typeToString(parentType)); + } } } return type || unknownType; @@ -4775,7 +4783,13 @@ module ts { return getTypeFromTypeNode(declaration.type); } if (declaration.kind === SyntaxKind.Parameter) { - return getContextuallyTypedParameterType(declaration); + var type = getContextuallyTypedParameterType(declaration); + if (type) { + return type; + } + } + if (isBindingPattern(declaration.name)) { + return getTypeFromBindingPattern(declaration.name); } } return undefined; diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 72a58797791..ffb09786c7e 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -275,7 +275,8 @@ module ts { 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." }, + Type_0_has_no_property_1: { code: 2459, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}'." }, + Type_0_has_no_numeric_index_signature: { code: 2460, category: DiagnosticCategory.Error, key: "Type '{0}' has 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 7a9deff62eb..132117414f7 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1096,10 +1096,14 @@ "category": "Error", "code": 2458 }, - "Type '{0}' has no property '{1}' and no numeric index signature.": { + "Type '{0}' has no property '{1}'.": { "category": "Error", "code": 2459 }, + "Type '{0}' has no numeric index signature.": { + "category": "Error", + "code": 2460 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error",