Skip to content

Commit 75a1a8a

Browse files
Disallow optional destructured parameters in implementation signatures.
1 parent dafe7c8 commit 75a1a8a

5 files changed

+18
-4
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_may_not_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
@@ -297,6 +297,7 @@ module ts {
297297
Type_0_has_no_property_1: { code: 2460, category: DiagnosticCategory.Error, key: "Type '{0}' has no property '{1}'." },
298298
Type_0_is_not_an_array_type: { code: 2461, category: DiagnosticCategory.Error, key: "Type '{0}' is not an array type." },
299299
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" },
300+
A_binding_pattern_parameter_may_not_be_optional_in_an_implementation_signature: { code: 2463, category: DiagnosticCategory.Error, key: "A binding pattern parameter may not be optional in an implementation signature." },
300301
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
301302
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}'." },
302303
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
"A 'declare' modifier cannot be used with an import declaration.": {
225225
"category": "Error",
226226
"code": 1079,
227-
"isEarly": true
227+
"isEarly": true
228228
},
229229
"Invalid 'reference' directive syntax.": {
230230
"category": "Error",
@@ -659,7 +659,7 @@
659659
"An implementation cannot be declared in ambient contexts.": {
660660
"category": "Error",
661661
"code": 1184,
662-
"isEarly": true
662+
"isEarly": true
663663
},
664664
"Modifiers cannot appear here.": {
665665
"category": "Error",
@@ -1282,6 +1282,10 @@
12821282
"category": "Error",
12831283
"code": 2462
12841284
},
1285+
"A binding pattern parameter may not be optional in an implementation signature.": {
1286+
"category": "Error",
1287+
"code": 2463
1288+
},
12851289

12861290
"Import declaration '{0}' is using private name '{1}'.": {
12871291
"category": "Error",

tests/baselines/reference/optionalBindingParameters1.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts(2,14): error TS2463: A binding pattern parameter may not be optional in an implementation signature.
12
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]'.
23

34

4-
==== tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/destructuring/optionalBindingParameters1.ts (2 errors) ====
56

67
function foo([x,y,z]?: [string, number, boolean]) {
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS2463: A binding pattern parameter may not be optional in an implementation signature.
710

811
}
912

tests/baselines/reference/optionalBindingParameters2.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts(2,14): error TS2463: A binding pattern parameter may not be optional in an implementation signature.
12
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; }'.
23
Types of property 'x' are incompatible.
34
Type 'boolean' is not assignable to type 'string'.
45

56

6-
==== tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts (1 errors) ====
7+
==== tests/cases/conformance/es6/destructuring/optionalBindingParameters2.ts (2 errors) ====
78

89
function foo({ x, y, z }?: { x: string; y: number; z: boolean }) {
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS2463: A binding pattern parameter may not be optional in an implementation signature.
912

1013
}
1114

0 commit comments

Comments
 (0)