Skip to content

Commit 39f04bf

Browse files
committed
fix(53933): forbid using initializer in destructured parameter in function type
1 parent 09b1c55 commit 39f04bf

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40928,7 +40928,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4092840928
forEach(node.name.elements, checkSourceElement);
4092940929
}
4093040930
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
40931-
if (isParameter(node) && node.initializer && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
40931+
if (hasInitializer(node) && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
4093240932
error(node, Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation);
4093340933
return;
4093440934
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
tests/cases/compiler/defaultValueInFunctionTypes.ts(1,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
2-
tests/cases/compiler/defaultValueInFunctionTypes.ts(2,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
1+
tests/cases/compiler/defaultValueInFunctionTypes.ts(1,15): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
2+
tests/cases/compiler/defaultValueInFunctionTypes.ts(3,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
3+
tests/cases/compiler/defaultValueInFunctionTypes.ts(4,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
34

45

5-
==== tests/cases/compiler/defaultValueInFunctionTypes.ts (2 errors) ====
6+
==== tests/cases/compiler/defaultValueInFunctionTypes.ts (3 errors) ====
7+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
8+
~~~~~
9+
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
10+
611
var x: (a: number = 1) => number;
712
~~~~~~~~~~~~~
813
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
914
var y = <(a : string = "") => any>(undefined)
1015
~~~~~~~~~~~~~~~
11-
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
16+
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
17+

tests/baselines/reference/defaultValueInFunctionTypes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//// [defaultValueInFunctionTypes.ts]
2+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
3+
24
var x: (a: number = 1) => number;
3-
var y = <(a : string = "") => any>(undefined)
5+
var y = <(a : string = "") => any>(undefined)
6+
47

58
//// [defaultValueInFunctionTypes.js]
69
var x;
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
=== tests/cases/compiler/defaultValueInFunctionTypes.ts ===
2+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
3+
>Foo : Symbol(Foo, Decl(defaultValueInFunctionTypes.ts, 0, 0))
4+
>first : Symbol(first, Decl(defaultValueInFunctionTypes.ts, 0, 13))
5+
>first : Symbol(first, Decl(defaultValueInFunctionTypes.ts, 0, 28))
6+
27
var x: (a: number = 1) => number;
3-
>x : Symbol(x, Decl(defaultValueInFunctionTypes.ts, 0, 3))
4-
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 0, 8))
8+
>x : Symbol(x, Decl(defaultValueInFunctionTypes.ts, 2, 3))
9+
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 2, 8))
510

611
var y = <(a : string = "") => any>(undefined)
7-
>y : Symbol(y, Decl(defaultValueInFunctionTypes.ts, 1, 3))
8-
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 1, 10))
12+
>y : Symbol(y, Decl(defaultValueInFunctionTypes.ts, 3, 3))
13+
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 3, 10))
914
>undefined : Symbol(undefined)
1015

tests/baselines/reference/defaultValueInFunctionTypes.types

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
=== tests/cases/compiler/defaultValueInFunctionTypes.ts ===
2+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
3+
>Foo : ({ first }: { first?: number; }) => unknown
4+
>first : number
5+
>0 : 0
6+
>first : number
7+
28
var x: (a: number = 1) => number;
39
>x : (a?: number) => number
410
>a : number
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
2+
13
var x: (a: number = 1) => number;
2-
var y = <(a : string = "") => any>(undefined)
4+
var y = <(a : string = "") => any>(undefined)

0 commit comments

Comments
 (0)