Skip to content

Commit b58ac4a

Browse files
Extract function types from function and arrow expressions. (#60234)
1 parent ef802b1 commit b58ac4a

File tree

78 files changed

+489
-385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+489
-385
lines changed

src/compiler/checker.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -6193,8 +6193,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
61936193
enterNewScope(context, node) {
61946194
if (isFunctionLike(node) || isJSDocSignature(node)) {
61956195
const signature = getSignatureFromDeclaration(node);
6196-
const expandedParams = getExpandedParameters(signature, /*skipUnionExpanding*/ true)[0];
6197-
return enterNewScope(context as NodeBuilderContext, node, expandedParams, signature.typeParameters);
6196+
return enterNewScope(context as NodeBuilderContext, node, signature.parameters, signature.typeParameters);
61986197
}
61996198
else {
62006199
const typeParameters = isConditionalTypeNode(node) ? getInferTypeParameters(node) :

src/compiler/expressionToTypeNode.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -969,14 +969,16 @@ export function createSyntacticTypeNodeBuilder(
969969
return failed;
970970
}
971971
function typeFromFunctionLikeExpression(fnNode: FunctionExpression | ArrowFunction, context: SyntacticTypeNodeBuilderContext) {
972-
// Disable any inference fallback since we won't actually use the resulting type and we don't want to generate errors
973-
const oldNoInferenceFallback = context.noInferenceFallback;
974-
context.noInferenceFallback = true;
975-
createReturnFromSignature(fnNode, /*symbol*/ undefined, context);
976-
reuseTypeParameters(fnNode.typeParameters, context);
977-
fnNode.parameters.map(p => ensureParameter(p, context));
978-
context.noInferenceFallback = oldNoInferenceFallback;
979-
return notImplemented;
972+
const returnType = createReturnFromSignature(fnNode, /*symbol*/ undefined, context);
973+
const typeParameters = reuseTypeParameters(fnNode.typeParameters, context);
974+
const parameters = fnNode.parameters.map(p => ensureParameter(p, context));
975+
return syntacticResult(
976+
factory.createFunctionTypeNode(
977+
typeParameters,
978+
parameters,
979+
returnType,
980+
),
981+
);
980982
}
981983
function canGetTypeFromArrayLiteral(arrayLiteral: ArrayLiteralExpression, context: SyntacticTypeNodeBuilderContext, isConstContext: boolean) {
982984
if (!isConstContext) {

tests/baselines/reference/arrowFunctionExpressions.types

+10-10
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ class MyClass {
245245
// Arrow function used in arrow function
246246
var arrrr = () => (m: number) => () => (n: number) => m + n;
247247
>arrrr : () => (m: number) => () => (n: number) => number
248-
> : ^^^^^^^ ^^ ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^
248+
> : ^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^
249249
>() => (m: number) => () => (n: number) => m + n : () => (m: number) => () => (n: number) => number
250-
> : ^^^^^^^ ^^ ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^
250+
> : ^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^
251251
>(m: number) => () => (n: number) => m + n : (m: number) => () => (n: number) => number
252-
> : ^ ^^ ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^
252+
> : ^ ^^ ^^^^^^^^^^^^ ^^^^^^^^^^^
253253
>m : number
254254
> : ^^^^^^
255255
>() => (n: number) => m + n : () => (n: number) => number
256-
> : ^^^^^^^ ^^ ^^^^^^^^^^^
256+
> : ^^^^^^^ ^^^^^^^^^^^
257257
>(n: number) => m + n : (n: number) => number
258258
> : ^ ^^ ^^^^^^^^^^^
259259
>n : number
@@ -273,11 +273,11 @@ var e = arrrr()(3)()(4);
273273
>arrrr()(3)() : (n: number) => number
274274
> : ^ ^^ ^^^^^^^^^^^
275275
>arrrr()(3) : () => (n: number) => number
276-
> : ^^^^^^^ ^^ ^^^^^^^^^^^
276+
> : ^^^^^^^ ^^^^^^^^^^^
277277
>arrrr() : (m: number) => () => (n: number) => number
278-
> : ^ ^^ ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^
278+
> : ^ ^^ ^^^^^^^^^^^^ ^^^^^^^^^^^
279279
>arrrr : () => (m: number) => () => (n: number) => number
280-
> : ^^^^^^^ ^^ ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^
280+
> : ^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^
281281
>3 : 3
282282
> : ^
283283
>4 : 4
@@ -294,9 +294,9 @@ function someFn() {
294294

295295
var arr = (n: number) => (p: number) => p * n;
296296
>arr : (n: number) => (p: number) => number
297-
> : ^ ^^ ^^^^^^ ^^ ^^^^^^^^^^^
297+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^
298298
>(n: number) => (p: number) => p * n : (n: number) => (p: number) => number
299-
> : ^ ^^ ^^^^^^ ^^ ^^^^^^^^^^^
299+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^
300300
>n : number
301301
> : ^^^^^^
302302
>(p: number) => p * n : (p: number) => number
@@ -320,7 +320,7 @@ function someFn() {
320320
>arr(3) : (p: number) => number
321321
> : ^ ^^ ^^^^^^^^^^^
322322
>arr : (n: number) => (p: number) => number
323-
> : ^ ^^ ^^^^^^ ^^ ^^^^^^^^^^^
323+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^
324324
>3 : 3
325325
> : ^
326326
>4 : 4

tests/baselines/reference/capturedParametersInInitializers1.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function foo1(y = class {c = x}, x = 1) {
3232
// ok - used in file
3333
function foo2(y = function(x: typeof z) {}, z = 1) {
3434
>foo2 : (y?: (x: typeof z) => void, z?: number) => void
35-
> : ^ ^^^^ ^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
35+
> : ^ ^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
3636
>y : (x: typeof z) => void
3737
> : ^ ^^ ^^^^^^^^^
3838
>function(x: typeof z) {} : (x: typeof z) => void

tests/baselines/reference/collisionSuperAndParameter.types

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class Foo {
1111

1212
var lamda = (_super: number) => { // No Error
1313
>lamda : (_super: number) => (x: any) => this
14-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^^
14+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^
1515
>(_super: number) => { // No Error return x => this; // New scope. So should inject new _this capture } : (_super: number) => (x: any) => this
16-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^^
16+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^
1717
>_super : number
1818
> : ^^^^^^
1919

@@ -33,9 +33,9 @@ class Foo {
3333

3434
var lambda = () => {
3535
>lambda : () => (x: any) => this
36-
> : ^^^^^^^ ^^^^^^^^^^^^^^
36+
> : ^^^^^^^ ^^^^^^^^^^^^
3737
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
38-
> : ^^^^^^^ ^^^^^^^^^^^^^^
38+
> : ^^^^^^^ ^^^^^^^^^^^^
3939

4040
return x => this; // New scope. So should inject new _this capture
4141
>x => this : (x: any) => this
@@ -64,9 +64,9 @@ class Foo2 extends Foo {
6464

6565
var lamda = (_super: number) => { // Error
6666
>lamda : (_super: number) => (x: any) => this
67-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^^
67+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^
6868
>(_super: number) => { // Error return x => this; // New scope. So should inject new _this capture } : (_super: number) => (x: any) => this
69-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^^
69+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^
7070
>_super : number
7171
> : ^^^^^^
7272

@@ -86,9 +86,9 @@ class Foo2 extends Foo {
8686

8787
var lambda = () => {
8888
>lambda : () => (x: any) => this
89-
> : ^^^^^^^ ^^^^^^^^^^^^^^
89+
> : ^^^^^^^ ^^^^^^^^^^^^
9090
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
91-
> : ^^^^^^^ ^^^^^^^^^^^^^^
91+
> : ^^^^^^^ ^^^^^^^^^^^^
9292

9393
return x => this; // New scope. So should inject new _this capture
9494
>x => this : (x: any) => this
@@ -218,9 +218,9 @@ class Foo4 extends Foo {
218218

219219
var lambda = () => {
220220
>lambda : () => (x: any) => this
221-
> : ^^^^^^^ ^^^^^^^^^^^^^^
221+
> : ^^^^^^^ ^^^^^^^^^^^^
222222
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
223-
> : ^^^^^^^ ^^^^^^^^^^^^^^
223+
> : ^^^^^^^ ^^^^^^^^^^^^
224224

225225
return x => this; // New scope. So should inject new _this capture
226226
>x => this : (x: any) => this

tests/baselines/reference/collisionThisExpressionAndNameResolution.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Foo {
2626

2727
function inner() {
2828
>inner : () => (x: any) => any
29-
> : ^^^^^^^ ^^^^^^^^^^^^^
29+
> : ^^^^^^^ ^^^^^^^^^^^
3030

3131
console.log(_this); // Error as this doesnt not resolve to user defined _this
3232
>console.log(_this) : any

tests/baselines/reference/collisionThisExpressionAndParameter.types

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Foo {
1717

1818
function inner(_this: number) { // Error
1919
>inner : (_this: number) => (x: any) => any
20-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^
20+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^
2121
>_this : number
2222
> : ^^^^^^
2323

@@ -34,9 +34,9 @@ class Foo {
3434

3535
var lamda = (_this: number) => { // Error
3636
>lamda : (_this: number) => (x: any) => this
37-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^^
37+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^
3838
>(_this: number) => { // Error return x => this; // New scope. So should inject new _this capture } : (_this: number) => (x: any) => this
39-
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^^^
39+
> : ^ ^^ ^^^^^^ ^^^^^^^^^^^^
4040
>_this : number
4141
> : ^^^^^^
4242

@@ -56,9 +56,9 @@ class Foo {
5656

5757
var lambda = () => {
5858
>lambda : () => (x: any) => this
59-
> : ^^^^^^^ ^^^^^^^^^^^^^^
59+
> : ^^^^^^^ ^^^^^^^^^^^^
6060
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
61-
> : ^^^^^^^ ^^^^^^^^^^^^^^
61+
> : ^^^^^^^ ^^^^^^^^^^^^
6262

6363
return x => this; // New scope. So should inject new _this capture
6464
>x => this : (x: any) => this
@@ -257,9 +257,9 @@ class Foo3 {
257257

258258
var lambda = () => {
259259
>lambda : () => (x: any) => this
260-
> : ^^^^^^^ ^^^^^^^^^^^^^^
260+
> : ^^^^^^^ ^^^^^^^^^^^^
261261
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
262-
> : ^^^^^^^ ^^^^^^^^^^^^^^
262+
> : ^^^^^^^ ^^^^^^^^^^^^
263263

264264
return x => this; // New scope. So should inject new _this capture
265265
>x => this : (x: any) => this

tests/baselines/reference/collisionThisExpressionAndPropertyNameAsConstuctorParameter.types

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class Foo2 {
1111

1212
var lambda = () => {
1313
>lambda : () => (x: any) => this
14-
> : ^^^^^^^ ^^^^^^^^^^^^^^
14+
> : ^^^^^^^ ^^^^^^^^^^^^
1515
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
16-
> : ^^^^^^^ ^^^^^^^^^^^^^^
16+
> : ^^^^^^^ ^^^^^^^^^^^^
1717

1818
return x => this; // New scope. So should inject new _this capture
1919
>x => this : (x: any) => this
@@ -35,9 +35,9 @@ class Foo3 {
3535

3636
var lambda = () => {
3737
>lambda : () => (x: any) => this
38-
> : ^^^^^^^ ^^^^^^^^^^^^^^
38+
> : ^^^^^^^ ^^^^^^^^^^^^
3939
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
40-
> : ^^^^^^^ ^^^^^^^^^^^^^^
40+
> : ^^^^^^^ ^^^^^^^^^^^^
4141

4242
return x => this; // New scope. So should inject new _this capture
4343
>x => this : (x: any) => this
@@ -66,9 +66,9 @@ class Foo4 {
6666

6767
var lambda = () => {
6868
>lambda : () => (x: any) => this
69-
> : ^^^^^^^ ^^^^^^^^^^^^^^
69+
> : ^^^^^^^ ^^^^^^^^^^^^
7070
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
71-
> : ^^^^^^^ ^^^^^^^^^^^^^^
71+
> : ^^^^^^^ ^^^^^^^^^^^^
7272

7373
return x => this; // New scope. So should inject new _this capture
7474
>x => this : (x: any) => this
@@ -97,9 +97,9 @@ class Foo5 {
9797

9898
var lambda = () => {
9999
>lambda : () => (x: any) => this
100-
> : ^^^^^^^ ^^^^^^^^^^^^^^
100+
> : ^^^^^^^ ^^^^^^^^^^^^
101101
>() => { return x => this; // New scope. So should inject new _this capture } : () => (x: any) => this
102-
> : ^^^^^^^ ^^^^^^^^^^^^^^
102+
> : ^^^^^^^ ^^^^^^^^^^^^
103103

104104
return x => this; // New scope. So should inject new _this capture
105105
>x => this : (x: any) => this

tests/baselines/reference/commentsFunction.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ b: number): void;
111111
/** fooFunc
112112
* comment
113113
*/
114-
declare var fooFunc: (b: string) => string;
115-
declare var lambdaFoo: (a: number, b: number) => number;
116-
declare var lambddaNoVarComment: (a: number, b: number) => number;
114+
declare var fooFunc: (/** fooFunctionValue param */ b: string) => string;
115+
declare var lambdaFoo: (/**param a*/ a: number, /**param b*/ b: number) => number;
116+
declare var lambddaNoVarComment: (/**param a*/ a: number, /**param b*/ b: number) => number;
117117
declare function blah(a: string): void;
118118
declare function blah2(a: string): void;
119119
declare function blah3(a: string): void;

tests/baselines/reference/contextualSignatureInstantiation2.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ var dot: <T, S>(f: (_: T) => S) => <U>(g: (_: U) => T) => (_: U) => S;
1818

1919
dot = <T, S>(f: (_: T) => S) => <U>(g: (_: U) => T): (r:U) => S => (x) => f(g(x));
2020
>dot = <T, S>(f: (_: T) => S) => <U>(g: (_: U) => T): (r:U) => S => (x) => f(g(x)) : <T, S>(f: (_: T) => S) => <U>(g: (_: U) => T) => (r: U) => S
21-
> : ^ ^^ ^^ ^^ ^^^^^^ ^^ ^^ ^^^^^
21+
> : ^ ^^ ^^ ^^ ^^^^^^ ^^ ^^^^^
2222
>dot : <T, S>(f: (_: T) => S) => <U>(g: (_: U) => T) => (_: U) => S
2323
> : ^ ^^ ^^ ^^ ^^^^^
2424
><T, S>(f: (_: T) => S) => <U>(g: (_: U) => T): (r:U) => S => (x) => f(g(x)) : <T, S>(f: (_: T) => S) => <U>(g: (_: U) => T) => (r: U) => S
25-
> : ^ ^^ ^^ ^^ ^^^^^^ ^^ ^^ ^^^^^
25+
> : ^ ^^ ^^ ^^ ^^^^^^ ^^ ^^^^^
2626
>f : (_: T) => S
2727
> : ^ ^^ ^^^^^
2828
>_ : T

tests/baselines/reference/contextualTypingOfAccessors.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ var x: {
1616

1717
x = {
1818
>x = { get foo() { return (n)=>n }, set foo(x) {}} : { foo: (n: any) => any; }
19-
> : ^^^^^^^^ ^^^^^^^^^^^^^^^^
19+
> : ^^^^^^^^ ^^^^^^^^^^^^^^
2020
>x : { foo: (x: number) => number; }
2121
> : ^^^^^^^ ^^^
2222
>{ get foo() { return (n)=>n }, set foo(x) {}} : { foo: (n: any) => any; }
23-
> : ^^^^^^^^ ^^^^^^^^^^^^^^^^
23+
> : ^^^^^^^^ ^^^^^^^^^^^^^^
2424

2525
get foo() {
2626
>foo : (n: any) => any

tests/baselines/reference/declFileTypeofFunction.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ declare function b1(): typeof b1;
7070
declare function foo(): typeof foo;
7171
declare var foo1: typeof foo;
7272
declare var foo2: typeof foo;
73-
declare var foo3: () => /*elided*/ any;
74-
declare var x: () => /*elided*/ any;
73+
declare var foo3: () => () => /*elided*/ any;
74+
declare var x: () => () => /*elided*/ any;
7575
declare function foo5(x: number): (x: number) => number;

tests/baselines/reference/declarationEmitArrowFunctionNoRenaming.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export type BoundedInteger<
2222

2323
export const toBoundedInteger =
2424
>toBoundedInteger : <LowerBound extends number, UpperBound extends number>(bounds: { lowerBound: LowerBound; upperBound: UpperBound; }) => (n: number) => BoundedInteger<LowerBound, UpperBound>
25-
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^^ ^^ ^^^^^
25+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^^ ^^^^^
2626

2727
<LowerBound extends number, UpperBound extends number>(bounds: {
2828
><LowerBound extends number, UpperBound extends number>(bounds: { lowerBound: LowerBound; upperBound: UpperBound; }) => ( n: number ): BoundedInteger<LowerBound, UpperBound> => // Implementation doesn't matter here ({} as any) : <LowerBound extends number, UpperBound extends number>(bounds: { lowerBound: LowerBound; upperBound: UpperBound; }) => (n: number) => BoundedInteger<LowerBound, UpperBound>
29-
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^^ ^^ ^^^^^
29+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^^ ^^^^^
3030
>bounds : { lowerBound: LowerBound; upperBound: UpperBound; }
3131
> : ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^
3232

tests/baselines/reference/declarationEmitBindingPatternsUnused.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function referencedInInferredType({ name: alias }: Named) {
169169

170170
function referencedInNestedFunction({ name: alias }: Named) {
171171
>referencedInNestedFunction : ({ name: alias }: Named) => (p: typeof alias) => void
172-
> : ^ ^^ ^^^^^^ ^^ ^^^^^^^^^
172+
> : ^ ^^ ^^^^^^ ^^^^^^^^^
173173
>name : any
174174
> : ^^^
175175
>alias : string

0 commit comments

Comments
 (0)