Skip to content

Commit 19e88ac

Browse files
authored
Merge pull request microsoft#14446 from Microsoft/circularVarTypeInference
Fix variable type inference circularity
2 parents 0afb84a + 56e2735 commit 19e88ac

9 files changed

+155
-27
lines changed

src/compiler/checker.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -16131,7 +16131,7 @@ namespace ts {
1613116131
}
1613216132

1613316133
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) {
16134-
const type = checkExpressionCached(declaration.initializer);
16134+
const type = getTypeOfExpression(declaration.initializer, /*cache*/ true);
1613516135
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
1613616136
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
1613716137
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
@@ -16204,10 +16204,12 @@ namespace ts {
1620416204

1620516205
// Returns the type of an expression. Unlike checkExpression, this function is simply concerned
1620616206
// with computing the type and may not fully check all contained sub-expressions for errors.
16207-
function getTypeOfExpression(node: Expression) {
16207+
// A cache argument of true indicates that if the function performs a full type check, it is ok
16208+
// to cache the result.
16209+
function getTypeOfExpression(node: Expression, cache?: boolean) {
1620816210
// Optimize for the common case of a call to a function with a single non-generic call
1620916211
// signature where we can just fetch the return type without checking the arguments.
16210-
if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword) {
16212+
if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
1621116213
const funcType = checkNonNullExpression((<CallExpression>node).expression);
1621216214
const signature = getSingleCallSignature(funcType);
1621316215
if (signature && !signature.typeParameters) {
@@ -16217,7 +16219,7 @@ namespace ts {
1621716219
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
1621816220
// should have a parameter that indicates whether full error checking is required such that
1621916221
// we can perform the optimizations locally.
16220-
return checkExpression(node);
16222+
return cache ? checkExpressionCached(node) : checkExpression(node);
1622116223
}
1622216224

1622316225
// Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When

tests/baselines/reference/ambientRequireFunction.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const fs = require("fs");
55
>fs : typeof "fs"
6-
>require("fs") : any
6+
>require("fs") : typeof "fs"
77
>require : (moduleName: string) => any
88
>"fs" : "fs"
99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [circularInferredTypeOfVariable.ts]
2+
3+
// Repro from #14428
4+
5+
(async () => {
6+
function foo(p: string[]): string[] {
7+
return [];
8+
}
9+
10+
function bar(p: string[]): string[] {
11+
return [];
12+
}
13+
14+
let a1: string[] | undefined = [];
15+
16+
while (true) {
17+
let a2 = foo(a1!);
18+
a1 = await bar(a2);
19+
}
20+
});
21+
22+
//// [circularInferredTypeOfVariable.js]
23+
// Repro from #14428
24+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
25+
return new (P || (P = Promise))(function (resolve, reject) {
26+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
29+
step((generator = generator.apply(thisArg, _arguments || [])).next());
30+
});
31+
};
32+
(() => __awaiter(this, void 0, void 0, function* () {
33+
function foo(p) {
34+
return [];
35+
}
36+
function bar(p) {
37+
return [];
38+
}
39+
let a1 = [];
40+
while (true) {
41+
let a2 = foo(a1);
42+
a1 = yield bar(a2);
43+
}
44+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/compiler/circularInferredTypeOfVariable.ts ===
2+
3+
// Repro from #14428
4+
5+
(async () => {
6+
function foo(p: string[]): string[] {
7+
>foo : Symbol(foo, Decl(circularInferredTypeOfVariable.ts, 3, 14))
8+
>p : Symbol(p, Decl(circularInferredTypeOfVariable.ts, 4, 17))
9+
10+
return [];
11+
}
12+
13+
function bar(p: string[]): string[] {
14+
>bar : Symbol(bar, Decl(circularInferredTypeOfVariable.ts, 6, 5))
15+
>p : Symbol(p, Decl(circularInferredTypeOfVariable.ts, 8, 17))
16+
17+
return [];
18+
}
19+
20+
let a1: string[] | undefined = [];
21+
>a1 : Symbol(a1, Decl(circularInferredTypeOfVariable.ts, 12, 7))
22+
23+
while (true) {
24+
let a2 = foo(a1!);
25+
>a2 : Symbol(a2, Decl(circularInferredTypeOfVariable.ts, 15, 11))
26+
>foo : Symbol(foo, Decl(circularInferredTypeOfVariable.ts, 3, 14))
27+
>a1 : Symbol(a1, Decl(circularInferredTypeOfVariable.ts, 12, 7))
28+
29+
a1 = await bar(a2);
30+
>a1 : Symbol(a1, Decl(circularInferredTypeOfVariable.ts, 12, 7))
31+
>bar : Symbol(bar, Decl(circularInferredTypeOfVariable.ts, 6, 5))
32+
>a2 : Symbol(a2, Decl(circularInferredTypeOfVariable.ts, 15, 11))
33+
}
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/compiler/circularInferredTypeOfVariable.ts ===
2+
3+
// Repro from #14428
4+
5+
(async () => {
6+
>(async () => { function foo(p: string[]): string[] { return []; } function bar(p: string[]): string[] { return []; } let a1: string[] | undefined = []; while (true) { let a2 = foo(a1!); a1 = await bar(a2); }}) : () => Promise<never>
7+
>async () => { function foo(p: string[]): string[] { return []; } function bar(p: string[]): string[] { return []; } let a1: string[] | undefined = []; while (true) { let a2 = foo(a1!); a1 = await bar(a2); }} : () => Promise<never>
8+
9+
function foo(p: string[]): string[] {
10+
>foo : (p: string[]) => string[]
11+
>p : string[]
12+
13+
return [];
14+
>[] : undefined[]
15+
}
16+
17+
function bar(p: string[]): string[] {
18+
>bar : (p: string[]) => string[]
19+
>p : string[]
20+
21+
return [];
22+
>[] : undefined[]
23+
}
24+
25+
let a1: string[] | undefined = [];
26+
>a1 : string[]
27+
>[] : undefined[]
28+
29+
while (true) {
30+
>true : true
31+
32+
let a2 = foo(a1!);
33+
>a2 : string[]
34+
>foo(a1!) : string[]
35+
>foo : (p: string[]) => string[]
36+
>a1! : string[]
37+
>a1 : string[]
38+
39+
a1 = await bar(a2);
40+
>a1 = await bar(a2) : string[]
41+
>a1 : string[]
42+
>await bar(a2) : string[]
43+
>bar(a2) : string[]
44+
>bar : (p: string[]) => string[]
45+
>a2 : string[]
46+
}
47+
});

tests/baselines/reference/controlFlowIterationErrors.errors.txt

+1-17
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(35,17): error
66
Type 'string' is not assignable to type 'number'.
77
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(46,17): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
88
Type 'string' is not assignable to type 'number'.
9-
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(77,13): error TS7022: 'y' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
10-
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(77,26): error TS2345: Argument of type 'string | number | boolean' is not assignable to parameter of type 'string | number'.
11-
Type 'true' is not assignable to type 'string | number'.
12-
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(88,13): error TS7022: 'y' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
13-
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(88,26): error TS2345: Argument of type 'string | number | boolean' is not assignable to parameter of type 'string | number'.
14-
Type 'true' is not assignable to type 'string | number'.
159

1610

17-
==== tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts (8 errors) ====
11+
==== tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts (4 errors) ====
1812

1913
let cond: boolean;
2014

@@ -104,11 +98,6 @@ tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(88,26): error
10498
x = "0";
10599
while (cond) {
106100
let y = asNumber(x);
107-
~
108-
!!! error TS7022: 'y' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
109-
~
110-
!!! error TS2345: Argument of type 'string | number | boolean' is not assignable to parameter of type 'string | number'.
111-
!!! error TS2345: Type 'true' is not assignable to type 'string | number'.
112101
x = y + 1;
113102
x;
114103
}
@@ -120,11 +109,6 @@ tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(88,26): error
120109
while (cond) {
121110
x;
122111
let y = asNumber(x);
123-
~
124-
!!! error TS7022: 'y' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
125-
~
126-
!!! error TS2345: Argument of type 'string | number | boolean' is not assignable to parameter of type 'string | number'.
127-
!!! error TS2345: Type 'true' is not assignable to type 'string | number'.
128112
x = y + 1;
129113
x;
130114
}

tests/baselines/reference/implicitAnyFromCircularInference.errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ tests/cases/compiler/implicitAnyFromCircularInference.ts(18,10): error TS7024: F
77
tests/cases/compiler/implicitAnyFromCircularInference.ts(23,10): error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
88
tests/cases/compiler/implicitAnyFromCircularInference.ts(26,10): error TS7023: 'h' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
99
tests/cases/compiler/implicitAnyFromCircularInference.ts(28,14): error TS7023: 'foo' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
10-
tests/cases/compiler/implicitAnyFromCircularInference.ts(41,5): error TS7022: 's' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
1110
tests/cases/compiler/implicitAnyFromCircularInference.ts(46,9): error TS7023: 'x' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
1211

1312

14-
==== tests/cases/compiler/implicitAnyFromCircularInference.ts (11 errors) ====
13+
==== tests/cases/compiler/implicitAnyFromCircularInference.ts (10 errors) ====
1514

1615
// Error expected
1716
var a: typeof a;
@@ -71,8 +70,6 @@ tests/cases/compiler/implicitAnyFromCircularInference.ts(46,9): error TS7023: 'x
7170
class C {
7271
// Error expected
7372
s = foo(this);
74-
~~~~~~~~~~~~~~
75-
!!! error TS7022: 's' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
7673
}
7774

7875
class D {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @target: es6
2+
3+
// Repro from #14428
4+
5+
(async () => {
6+
function foo(p: string[]): string[] {
7+
return [];
8+
}
9+
10+
function bar(p: string[]): string[] {
11+
return [];
12+
}
13+
14+
let a1: string[] | undefined = [];
15+
16+
while (true) {
17+
let a2 = foo(a1!);
18+
a1 = await bar(a2);
19+
}
20+
});

tests/cases/fourslash/extendArrayInterfaceMember.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ verify.numberOfErrorsInCurrentFile(1);
1010
// - Supplied parameters do not match any signature of call target.
1111
// - Could not select overload for 'call' expression.
1212

13-
verify.quickInfoAt("y", "var y: any");
13+
verify.quickInfoAt("y", "var y: number");
1414

1515
goTo.eof();
1616
edit.insert("interface Array<T> { pop(def: T): T; }");

0 commit comments

Comments
 (0)