Skip to content

Commit 5bcb52f

Browse files
Merge pull request #1676 from Microsoft/disallowOptionalBindingParameters
Disallow optional destructured parameters in implementation signatures.
2 parents 778c91e + 2a11222 commit 5bcb52f

15 files changed

+186
-0
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7212,6 +7212,9 @@ module ts {
72127212
error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
72137213
}
72147214
}
7215+
if (node.questionToken && isBindingPattern(node.name) && func.body) {
7216+
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
7217+
}
72157218
if (node.dotDotDotToken) {
72167219
if (!isArrayType(getTypeOfSymbol(node.symbol))) {
72177220
error(node, Diagnostics.A_rest_parameter_must_be_of_an_array_type);

src/compiler/diagnosticInformationMap.generated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ module ts {
298298
Type_0_has_no_property_1: { code: 2460, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}'." },
299299
Type_0_is_not_an_array_type: { code: 2461, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type." },
300300
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" },
301+
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." },
301302
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
302303
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}'." },
303304
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}'." },

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,10 @@
12861286
"category": "Error",
12871287
"code": 2462
12881288
},
1289+
"A binding pattern parameter cannot be optional in an implementation signature.": {
1290+
"category": "Error",
1291+
"code": 2463
1292+
},
12891293

12901294
"Import declaration '{0}' is using private name '{1}'.": {
12911295
"category": "Error",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts(2,14): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
2+
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]'.
3+
4+
5+
==== tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts (2 errors) ====
6+
7+
function foo([x,y,z]?: [string, number, boolean]) {
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
10+
11+
}
12+
13+
foo(["", 0, false]);
14+
15+
foo([false, 0, ""]);
16+
~~~~~~~~~~~~~~
17+
!!! error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [optionalBindingParameters1.ts]
2+
3+
function foo([x,y,z]?: [string, number, boolean]) {
4+
5+
}
6+
7+
foo(["", 0, false]);
8+
9+
foo([false, 0, ""]);
10+
11+
//// [optionalBindingParameters1.js]
12+
function foo(_a) {
13+
var x = _a[0], y = _a[1], z = _a[2];
14+
}
15+
foo(["", 0, false]);
16+
foo([false, 0, ""]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts(2,14): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
2+
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; }'.
3+
Types of property 'x' are incompatible.
4+
Type 'boolean' is not assignable to type 'string'.
5+
6+
7+
==== tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts (2 errors) ====
8+
9+
function foo({ x, y, z }?: { x: string; y: number; z: boolean }) {
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
12+
13+
}
14+
15+
foo({ x: "", y: 0, z: false });
16+
17+
foo({ x: false, y: 0, z: "" });
18+
~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'.
20+
!!! error TS2345: Types of property 'x' are incompatible.
21+
!!! error TS2345: Type 'boolean' is not assignable to type 'string'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [optionalBindingParameters2.ts]
2+
3+
function foo({ x, y, z }?: { x: string; y: number; z: boolean }) {
4+
5+
}
6+
7+
foo({ x: "", y: 0, z: false });
8+
9+
foo({ x: false, y: 0, z: "" });
10+
11+
//// [optionalBindingParameters2.js]
12+
function foo(_a) {
13+
var x = _a.x, y = _a.y, z = _a.z;
14+
}
15+
foo({ x: "", y: 0, z: false });
16+
foo({ x: false, y: 0, z: "" });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
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]'.
2+
3+
4+
==== tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads1.ts (1 errors) ====
5+
6+
function foo([x, y, z] ?: [string, number, boolean]);
7+
function foo(...rest: any[]) {
8+
9+
}
10+
11+
foo(["", 0, false]);
12+
13+
foo([false, 0, ""]);
14+
~~~~~~~~~~~~~~
15+
!!! error TS2345: Argument of type '[boolean, number, string]' is not assignable to parameter of type '[string, number, boolean]'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [optionalBindingParametersInOverloads1.ts]
2+
3+
function foo([x, y, z] ?: [string, number, boolean]);
4+
function foo(...rest: any[]) {
5+
6+
}
7+
8+
foo(["", 0, false]);
9+
10+
foo([false, 0, ""]);
11+
12+
//// [optionalBindingParametersInOverloads1.js]
13+
function foo() {
14+
var rest = [];
15+
for (var _i = 0; _i < arguments.length; _i++) {
16+
rest[_i - 0] = arguments[_i];
17+
}
18+
}
19+
foo(["", 0, false]);
20+
foo([false, 0, ""]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
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; }'.
2+
Types of property 'x' are incompatible.
3+
Type 'boolean' is not assignable to type 'string'.
4+
5+
6+
==== tests/cases/conformance/es6/destructuring/optionalBindingParametersInOverloads2.ts (1 errors) ====
7+
8+
function foo({ x, y, z }?: { x: string; y: number; z: boolean });
9+
function foo(...rest: any[]) {
10+
11+
}
12+
13+
foo({ x: "", y: 0, z: false });
14+
15+
foo({ x: false, y: 0, z: "" });
16+
~~~~~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS2345: Argument of type '{ x: boolean; y: number; z: string; }' is not assignable to parameter of type '{ x: string; y: number; z: boolean; }'.
18+
!!! error TS2345: Types of property 'x' are incompatible.
19+
!!! error TS2345: Type 'boolean' is not assignable to type 'string'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [optionalBindingParametersInOverloads2.ts]
2+
3+
function foo({ x, y, z }?: { x: string; y: number; z: boolean });
4+
function foo(...rest: any[]) {
5+
6+
}
7+
8+
foo({ x: "", y: 0, z: false });
9+
10+
foo({ x: false, y: 0, z: "" });
11+
12+
//// [optionalBindingParametersInOverloads2.js]
13+
function foo() {
14+
var rest = [];
15+
for (var _i = 0; _i < arguments.length; _i++) {
16+
rest[_i - 0] = arguments[_i];
17+
}
18+
}
19+
foo({ x: "", y: 0, z: false });
20+
foo({ x: false, y: 0, z: "" });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+

2+
function foo([x,y,z]?: [string, number, boolean]) {
3+
4+
}
5+
6+
foo(["", 0, false]);
7+
8+
foo([false, 0, ""]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+

2+
function foo({ x, y, z }?: { x: string; y: number; z: boolean }) {
3+
4+
}
5+
6+
foo({ x: "", y: 0, z: false });
7+
8+
foo({ x: false, y: 0, z: "" });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+

2+
function foo([x, y, z] ?: [string, number, boolean]);
3+
function foo(...rest: any[]) {
4+
5+
}
6+
7+
foo(["", 0, false]);
8+
9+
foo([false, 0, ""]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+

2+
function foo({ x, y, z }?: { x: string; y: number; z: boolean });
3+
function foo(...rest: any[]) {
4+
5+
}
6+
7+
foo({ x: "", y: 0, z: false });
8+
9+
foo({ x: false, y: 0, z: "" });

0 commit comments

Comments
 (0)