Skip to content

Commit 60ce788

Browse files
authored
fix(53933): Confusing rules around function parameter names in a type (#53946)
1 parent d7c2670 commit 60ce788

6 files changed

+33
-11
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42152,7 +42152,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4215242152
forEach(node.name.elements, checkSourceElement);
4215342153
}
4215442154
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
42155-
if (isParameter(node) && node.initializer && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
42155+
if (node.initializer && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
4215642156
error(node, Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation);
4215742157
return;
4215842158
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
defaultValueInFunctionTypes.ts(1,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
2-
defaultValueInFunctionTypes.ts(2,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
1+
defaultValueInFunctionTypes.ts(1,15): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
2+
defaultValueInFunctionTypes.ts(3,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
3+
defaultValueInFunctionTypes.ts(4,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
34

45

5-
==== defaultValueInFunctionTypes.ts (2 errors) ====
6+
==== 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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//// [tests/cases/compiler/defaultValueInFunctionTypes.ts] ////
22

33
//// [defaultValueInFunctionTypes.ts]
4+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
5+
46
var x: (a: number = 1) => number;
5-
var y = <(a : string = "") => any>(undefined)
7+
var y = <(a : string = "") => any>(undefined)
8+
69

710
//// [defaultValueInFunctionTypes.js]
811
var x;
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
//// [tests/cases/compiler/defaultValueInFunctionTypes.ts] ////
22

33
=== defaultValueInFunctionTypes.ts ===
4+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
5+
>Foo : Symbol(Foo, Decl(defaultValueInFunctionTypes.ts, 0, 0))
6+
>first : Symbol(first, Decl(defaultValueInFunctionTypes.ts, 0, 13))
7+
>first : Symbol(first, Decl(defaultValueInFunctionTypes.ts, 0, 28))
8+
49
var x: (a: number = 1) => number;
5-
>x : Symbol(x, Decl(defaultValueInFunctionTypes.ts, 0, 3))
6-
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 0, 8))
10+
>x : Symbol(x, Decl(defaultValueInFunctionTypes.ts, 2, 3))
11+
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 2, 8))
712

813
var y = <(a : string = "") => any>(undefined)
9-
>y : Symbol(y, Decl(defaultValueInFunctionTypes.ts, 1, 3))
10-
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 1, 10))
14+
>y : Symbol(y, Decl(defaultValueInFunctionTypes.ts, 3, 3))
15+
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 3, 10))
1116
>undefined : Symbol(undefined)
1217

tests/baselines/reference/defaultValueInFunctionTypes.types

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
//// [tests/cases/compiler/defaultValueInFunctionTypes.ts] ////
22

33
=== defaultValueInFunctionTypes.ts ===
4+
type Foo = ({ first = 0 }: { first?: number }) => unknown;
5+
>Foo : ({ first }: { first?: number; }) => unknown
6+
>first : number
7+
>0 : 0
8+
>first : number
9+
410
var x: (a: number = 1) => number;
511
>x : (a?: number) => number
612
>a : number
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)