diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e1749ce88d0..3ad62c1d639 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7212,6 +7212,9 @@ module ts { error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); } } + if (node.questionToken && isBindingPattern(node.name) && func.body) { + error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature); + } if (node.dotDotDotToken) { if (!isArrayType(getTypeOfSymbol(node.symbol))) { error(node, Diagnostics.A_rest_parameter_must_be_of_an_array_type); diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 80e1505569d..9d62ee0e030 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -298,6 +298,7 @@ module ts { Type_0_has_no_property_1: { code: 2460, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}'." }, Type_0_is_not_an_array_type: { code: 2461, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type." }, A_rest_element_must_be_last_in_an_array_destructuring_pattern: { code: 2462, category: DiagnosticCategory.Error, key: "A rest element must be last in an array destructuring pattern" }, + A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature: { code: 2463, category: DiagnosticCategory.Error, key: "A binding pattern parameter cannot be optional in an implementation 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_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 704e3ada25f..ee962edc3a0 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1286,6 +1286,10 @@ "category": "Error", "code": 2462 }, + "A binding pattern parameter cannot be optional in an implementation signature.": { + "category": "Error", + "code": 2463 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/optionalBindingParameters1.errors.txt b/tests/baselines/reference/optionalBindingParameters1.errors.txt new file mode 100644 index 00000000000..a2961fb9651 --- /dev/null +++ b/tests/baselines/reference/optionalBindingParameters1.errors.txt @@ -0,0 +1,17 @@ +tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts(2,14): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts(8,5): error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'. + + +==== tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts (2 errors) ==== + + function foo([x,y,z]?: [string, number, boolean]) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + + } + + foo(["", 0, false]); + + foo([false, 0, ""]); + ~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'. \ No newline at end of file diff --git a/tests/baselines/reference/optionalBindingParameters1.js b/tests/baselines/reference/optionalBindingParameters1.js new file mode 100644 index 00000000000..536cb1e567a --- /dev/null +++ b/tests/baselines/reference/optionalBindingParameters1.js @@ -0,0 +1,16 @@ +//// [optionalBindingParameters1.ts] + +function foo([x,y,z]?: [string, number, boolean]) { + +} + +foo(["", 0, false]); + +foo([false, 0, ""]); + +//// [optionalBindingParameters1.js] +function foo(_a) { + var x = _a[0], y = _a[1], z = _a[2]; +} +foo(["", 0, false]); +foo([false, 0, ""]); diff --git a/tests/baselines/reference/optionalBindingParameters2.errors.txt b/tests/baselines/reference/optionalBindingParameters2.errors.txt new file mode 100644 index 00000000000..e67687722d0 --- /dev/null +++ b/tests/baselines/reference/optionalBindingParameters2.errors.txt @@ -0,0 +1,21 @@ +tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts(2,14): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts(8,5): error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'. + Types of property 'x' are incompatible. + Type 'boolean' is not assignable to type 'string'. + + +==== tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts (2 errors) ==== + + function foo({ x, y, z }?: { x: string; y: number; z: boolean }) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + + } + + foo({ x: "", y: 0, z: false }); + + foo({ x: false, y: 0, z: "" }); + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'. +!!! error TS2345: Types of property 'x' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/optionalBindingParameters2.js b/tests/baselines/reference/optionalBindingParameters2.js new file mode 100644 index 00000000000..04e1138561a --- /dev/null +++ b/tests/baselines/reference/optionalBindingParameters2.js @@ -0,0 +1,16 @@ +//// [optionalBindingParameters2.ts] + +function foo({ x, y, z }?: { x: string; y: number; z: boolean }) { + +} + +foo({ x: "", y: 0, z: false }); + +foo({ x: false, y: 0, z: "" }); + +//// [optionalBindingParameters2.js] +function foo(_a) { + var x = _a.x, y = _a.y, z = _a.z; +} +foo({ x: "", y: 0, z: false }); +foo({ x: false, y: 0, z: "" }); diff --git a/tests/baselines/reference/optionalBindingParametersInOverloads1.errors.txt b/tests/baselines/reference/optionalBindingParametersInOverloads1.errors.txt new file mode 100644 index 00000000000..5999ec514b0 --- /dev/null +++ b/tests/baselines/reference/optionalBindingParametersInOverloads1.errors.txt @@ -0,0 +1,15 @@ +tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads1.ts(9,5): error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'. + + +==== tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads1.ts (1 errors) ==== + + function foo([x, y, z] ?: [string, number, boolean]); + function foo(...rest: any[]) { + + } + + foo(["", 0, false]); + + foo([false, 0, ""]); + ~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'. \ No newline at end of file diff --git a/tests/baselines/reference/optionalBindingParametersInOverloads1.js b/tests/baselines/reference/optionalBindingParametersInOverloads1.js new file mode 100644 index 00000000000..3658efa72c6 --- /dev/null +++ b/tests/baselines/reference/optionalBindingParametersInOverloads1.js @@ -0,0 +1,20 @@ +//// [optionalBindingParametersInOverloads1.ts] + +function foo([x, y, z] ?: [string, number, boolean]); +function foo(...rest: any[]) { + +} + +foo(["", 0, false]); + +foo([false, 0, ""]); + +//// [optionalBindingParametersInOverloads1.js] +function foo() { + var rest = []; + for (var _i = 0; _i < arguments.length; _i++) { + rest[_i - 0] = arguments[_i]; + } +} +foo(["", 0, false]); +foo([false, 0, ""]); diff --git a/tests/baselines/reference/optionalBindingParametersInOverloads2.errors.txt b/tests/baselines/reference/optionalBindingParametersInOverloads2.errors.txt new file mode 100644 index 00000000000..765fd5e3de6 --- /dev/null +++ b/tests/baselines/reference/optionalBindingParametersInOverloads2.errors.txt @@ -0,0 +1,19 @@ +tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads2.ts(9,5): error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'. + Types of property 'x' are incompatible. + Type 'boolean' is not assignable to type 'string'. + + +==== tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads2.ts (1 errors) ==== + + function foo({ x, y, z }?: { x: string; y: number; z: boolean }); + function foo(...rest: any[]) { + + } + + foo({ x: "", y: 0, z: false }); + + foo({ x: false, y: 0, z: "" }); + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'. +!!! error TS2345: Types of property 'x' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/optionalBindingParametersInOverloads2.js b/tests/baselines/reference/optionalBindingParametersInOverloads2.js new file mode 100644 index 00000000000..1ddfdae4f07 --- /dev/null +++ b/tests/baselines/reference/optionalBindingParametersInOverloads2.js @@ -0,0 +1,20 @@ +//// [optionalBindingParametersInOverloads2.ts] + +function foo({ x, y, z }?: { x: string; y: number; z: boolean }); +function foo(...rest: any[]) { + +} + +foo({ x: "", y: 0, z: false }); + +foo({ x: false, y: 0, z: "" }); + +//// [optionalBindingParametersInOverloads2.js] +function foo() { + var rest = []; + for (var _i = 0; _i < arguments.length; _i++) { + rest[_i - 0] = arguments[_i]; + } +} +foo({ x: "", y: 0, z: false }); +foo({ x: false, y: 0, z: "" }); diff --git a/tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts b/tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts new file mode 100644 index 00000000000..5bdcb49e5d9 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts @@ -0,0 +1,8 @@ + +function foo([x,y,z]?: [string, number, boolean]) { + +} + +foo(["", 0, false]); + +foo([false, 0, ""]); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts b/tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts new file mode 100644 index 00000000000..12338aeb445 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts @@ -0,0 +1,8 @@ + +function foo({ x, y, z }?: { x: string; y: number; z: boolean }) { + +} + +foo({ x: "", y: 0, z: false }); + +foo({ x: false, y: 0, z: "" }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads1.ts b/tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads1.ts new file mode 100644 index 00000000000..82aa49d346a --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads1.ts @@ -0,0 +1,9 @@ + +function foo([x, y, z] ?: [string, number, boolean]); +function foo(...rest: any[]) { + +} + +foo(["", 0, false]); + +foo([false, 0, ""]); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads2.ts b/tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads2.ts new file mode 100644 index 00000000000..bbe763b3008 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads2.ts @@ -0,0 +1,9 @@ + +function foo({ x, y, z }?: { x: string; y: number; z: boolean }); +function foo(...rest: any[]) { + +} + +foo({ x: "", y: 0, z: false }); + +foo({ x: false, y: 0, z: "" }); \ No newline at end of file