Skip to content

Commit ae7d232

Browse files
authored
fix(49869): throw an error on optional binding pattern parameter in JavaScript (#50094)
1 parent 88a1e3a commit ae7d232

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35230,7 +35230,7 @@ namespace ts {
3523035230
error(node.name, Diagnostics.constructor_cannot_be_used_as_a_parameter_property_name);
3523135231
}
3523235232
}
35233-
if (node.questionToken && isBindingPattern(node.name) && (func as FunctionLikeDeclaration).body) {
35233+
if ((node.questionToken || isJSDocOptionalParameter(node)) && isBindingPattern(node.name) && (func as FunctionLikeDeclaration).body) {
3523435234
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);
3523535235
}
3523635236
if (node.name && isIdentifier(node.name) && (node.name.escapedText === "this" || node.name.escapedText === "new")) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/a.js(9,12): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
2+
3+
4+
==== /a.js (1 errors) ====
5+
/**
6+
* @typedef Foo
7+
* @property {string} a
8+
*/
9+
10+
/**
11+
* @param {Foo} [options]
12+
*/
13+
function f({ a = "a" }) {}
14+
~~~~~~~~~~~
15+
!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== /a.js ===
2+
/**
3+
* @typedef Foo
4+
* @property {string} a
5+
*/
6+
7+
/**
8+
* @param {Foo} [options]
9+
*/
10+
function f({ a = "a" }) {}
11+
>f : Symbol(f, Decl(a.js, 0, 0))
12+
>a : Symbol(a, Decl(a.js, 8, 12))
13+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== /a.js ===
2+
/**
3+
* @typedef Foo
4+
* @property {string} a
5+
*/
6+
7+
/**
8+
* @param {Foo} [options]
9+
*/
10+
function f({ a = "a" }) {}
11+
>f : ({ a }?: Foo) => void
12+
>a : string
13+
>"a" : "a"
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @checkJs: true
2+
// @allowJs: true
3+
// @noEmit: true
4+
// @filename: /a.js
5+
6+
/**
7+
* @typedef Foo
8+
* @property {string} a
9+
*/
10+
11+
/**
12+
* @param {Foo} [options]
13+
*/
14+
function f({ a = "a" }) {}

0 commit comments

Comments
 (0)