fix(53933): Confusing rules around function parameter names in a type (#53946)

This commit is contained in:
Oleksandr T 2023-10-30 21:24:14 +02:00 committed by GitHub
parent d7c2670898
commit 60ce788302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 11 deletions

View File

@ -42152,7 +42152,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
forEach(node.name.elements, checkSourceElement);
}
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
if (isParameter(node) && node.initializer && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
if (node.initializer && isParameterDeclaration(node) && nodeIsMissing((getContainingFunction(node) as FunctionLikeDeclaration).body)) {
error(node, Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation);
return;
}

View File

@ -1,11 +1,17 @@
defaultValueInFunctionTypes.ts(1,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
defaultValueInFunctionTypes.ts(2,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
defaultValueInFunctionTypes.ts(1,15): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
defaultValueInFunctionTypes.ts(3,9): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
defaultValueInFunctionTypes.ts(4,11): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
==== defaultValueInFunctionTypes.ts (2 errors) ====
==== defaultValueInFunctionTypes.ts (3 errors) ====
type Foo = ({ first = 0 }: { first?: number }) => unknown;
~~~~~
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
var x: (a: number = 1) => number;
~~~~~~~~~~~~~
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
var y = <(a : string = "") => any>(undefined)
~~~~~~~~~~~~~~~
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation.

View File

@ -1,8 +1,11 @@
//// [tests/cases/compiler/defaultValueInFunctionTypes.ts] ////
//// [defaultValueInFunctionTypes.ts]
type Foo = ({ first = 0 }: { first?: number }) => unknown;
var x: (a: number = 1) => number;
var y = <(a : string = "") => any>(undefined)
var y = <(a : string = "") => any>(undefined)
//// [defaultValueInFunctionTypes.js]
var x;

View File

@ -1,12 +1,17 @@
//// [tests/cases/compiler/defaultValueInFunctionTypes.ts] ////
=== defaultValueInFunctionTypes.ts ===
type Foo = ({ first = 0 }: { first?: number }) => unknown;
>Foo : Symbol(Foo, Decl(defaultValueInFunctionTypes.ts, 0, 0))
>first : Symbol(first, Decl(defaultValueInFunctionTypes.ts, 0, 13))
>first : Symbol(first, Decl(defaultValueInFunctionTypes.ts, 0, 28))
var x: (a: number = 1) => number;
>x : Symbol(x, Decl(defaultValueInFunctionTypes.ts, 0, 3))
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 0, 8))
>x : Symbol(x, Decl(defaultValueInFunctionTypes.ts, 2, 3))
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 2, 8))
var y = <(a : string = "") => any>(undefined)
>y : Symbol(y, Decl(defaultValueInFunctionTypes.ts, 1, 3))
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 1, 10))
>y : Symbol(y, Decl(defaultValueInFunctionTypes.ts, 3, 3))
>a : Symbol(a, Decl(defaultValueInFunctionTypes.ts, 3, 10))
>undefined : Symbol(undefined)

View File

@ -1,6 +1,12 @@
//// [tests/cases/compiler/defaultValueInFunctionTypes.ts] ////
=== defaultValueInFunctionTypes.ts ===
type Foo = ({ first = 0 }: { first?: number }) => unknown;
>Foo : ({ first }: { first?: number; }) => unknown
>first : number
>0 : 0
>first : number
var x: (a: number = 1) => number;
>x : (a?: number) => number
>a : number

View File

@ -1,2 +1,4 @@
type Foo = ({ first = 0 }: { first?: number }) => unknown;
var x: (a: number = 1) => number;
var y = <(a : string = "") => any>(undefined)
var y = <(a : string = "") => any>(undefined)