Skip to content

Commit f126767

Browse files
committed
Merge pull request microsoft#3724 from Microsoft/apparentTypeOfContextualType
Always get the apparent type when retrieving a contextual type
2 parents 19c332d + 6529951 commit f126767

21 files changed

+391
-36
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6442,6 +6442,11 @@ namespace ts {
64426442
// Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
64436443
// be "pushed" onto a node using the contextualType property.
64446444
function getContextualType(node: Expression): Type {
6445+
let type = getContextualTypeWorker(node);
6446+
return type && getApparentType(type);
6447+
}
6448+
6449+
function getContextualTypeWorker(node: Expression): Type {
64456450
if (isInsideWithStatementBody(node)) {
64466451
// We cannot answer semantic questions within a with block, do not proceed any further
64476452
return undefined;

tests/baselines/reference/compoundAdditionAssignmentLHSCanBeAssigned.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ x2 += E.a;
9898
x2 += {};
9999
>x2 += {} : string
100100
>x2 : string
101-
>{} : {}
101+
>{} : { [x: number]: undefined; }
102102

103103
x2 += null;
104104
>x2 += null : string

tests/baselines/reference/contextualTypeWithUnionTypeCallSignatures.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,7 @@ var x4: IWithCallSignatures | IWithCallSignatures4 = a => /*here a should be any
8282
>IWithCallSignatures : Symbol(IWithCallSignatures, Decl(contextualTypeWithUnionTypeCallSignatures.ts, 9, 1))
8383
>IWithCallSignatures4 : Symbol(IWithCallSignatures4, Decl(contextualTypeWithUnionTypeCallSignatures.ts, 18, 1))
8484
>a : Symbol(a, Decl(contextualTypeWithUnionTypeCallSignatures.ts, 35, 52))
85+
>a.toString : Symbol(Number.toString, Decl(lib.d.ts, 458, 18))
8586
>a : Symbol(a, Decl(contextualTypeWithUnionTypeCallSignatures.ts, 35, 52))
87+
>toString : Symbol(Number.toString, Decl(lib.d.ts, 458, 18))
8688

tests/baselines/reference/contextualTypeWithUnionTypeCallSignatures.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ var x4: IWithCallSignatures | IWithCallSignatures4 = a => /*here a should be any
9090
>x4 : IWithCallSignatures | IWithCallSignatures4
9191
>IWithCallSignatures : IWithCallSignatures
9292
>IWithCallSignatures4 : IWithCallSignatures4
93-
>a => /*here a should be any*/ a.toString() : (a: any) => any
94-
>a : any
95-
>a.toString() : any
96-
>a.toString : any
97-
>a : any
98-
>toString : any
93+
>a => /*here a should be any*/ a.toString() : (a: number) => string
94+
>a : number
95+
>a.toString() : string
96+
>a.toString : (radix?: number) => string
97+
>a : number
98+
>toString : (radix?: number) => string
9999

tests/baselines/reference/functionConstraintSatisfaction3.types

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ var c: { (): string; (x): string };
3737
>x : any
3838

3939
var r1 = foo((x) => x);
40-
>r1 : (x: any) => any
41-
>foo((x) => x) : (x: any) => any
40+
>r1 : (x: string) => string
41+
>foo((x) => x) : (x: string) => string
4242
>foo : <T extends (x: string) => string>(x: T) => T
43-
>(x) => x : (x: any) => any
44-
>x : any
45-
>x : any
43+
>(x) => x : (x: string) => string
44+
>x : string
45+
>x : string
4646

4747
var r2 = foo((x: string) => x);
4848
>r2 : (x: string) => string
@@ -53,12 +53,12 @@ var r2 = foo((x: string) => x);
5353
>x : string
5454

5555
var r3 = foo(function (x) { return x });
56-
>r3 : (x: any) => any
57-
>foo(function (x) { return x }) : (x: any) => any
56+
>r3 : (x: string) => string
57+
>foo(function (x) { return x }) : (x: string) => string
5858
>foo : <T extends (x: string) => string>(x: T) => T
59-
>function (x) { return x } : (x: any) => any
60-
>x : any
61-
>x : any
59+
>function (x) { return x } : (x: string) => string
60+
>x : string
61+
>x : string
6262

6363
var r4 = foo(function (x: string) { return x });
6464
>r4 : (x: string) => string
@@ -130,8 +130,8 @@ var c2: { <T>(x: T): T; <T>(x: T, y: T): T };
130130
>T : T
131131

132132
var r9 = foo(function <U>(x: U) { return x; });
133-
>r9 : <U>(x: U) => U
134-
>foo(function <U>(x: U) { return x; }) : <U>(x: U) => U
133+
>r9 : (x: string) => string
134+
>foo(function <U>(x: U) { return x; }) : (x: string) => string
135135
>foo : <T extends (x: string) => string>(x: T) => T
136136
>function <U>(x: U) { return x; } : <U>(x: U) => U
137137
>U : U
@@ -140,8 +140,8 @@ var r9 = foo(function <U>(x: U) { return x; });
140140
>x : U
141141

142142
var r10 = foo(<U extends string>(x: U) => x);
143-
>r10 : <U extends string>(x: U) => U
144-
>foo(<U extends string>(x: U) => x) : <U extends string>(x: U) => U
143+
>r10 : (x: string) => string
144+
>foo(<U extends string>(x: U) => x) : (x: string) => string
145145
>foo : <T extends (x: string) => string>(x: T) => T
146146
><U extends string>(x: U) => x : <U extends string>(x: U) => U
147147
>U : U

tests/baselines/reference/genericCallWithTupleType.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTup
99
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(22,1): error TS2322: Type '[number, string]' is not assignable to type '[string, number]'.
1010
Types of property '0' are incompatible.
1111
Type 'number' is not assignable to type 'string'.
12-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(23,1): error TS2322: Type '[{}, {}]' is not assignable to type '[string, number]'.
12+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(23,1): error TS2322: Type '[{ [x: number]: undefined; }, {}]' is not assignable to type '[string, number]'.
1313
Types of property '0' are incompatible.
14-
Type '{}' is not assignable to type 'string'.
14+
Type '{ [x: number]: undefined; }' is not assignable to type 'string'.
1515
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTupleType.ts(24,1): error TS2322: Type '[{}]' is not assignable to type '[{}, {}]'.
1616
Property '1' is missing in type '[{}]'.
1717

@@ -55,9 +55,9 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithTup
5555
!!! error TS2322: Type 'number' is not assignable to type 'string'.
5656
i1.tuple1 = [{}, {}];
5757
~~~~~~~~~
58-
!!! error TS2322: Type '[{}, {}]' is not assignable to type '[string, number]'.
58+
!!! error TS2322: Type '[{ [x: number]: undefined; }, {}]' is not assignable to type '[string, number]'.
5959
!!! error TS2322: Types of property '0' are incompatible.
60-
!!! error TS2322: Type '{}' is not assignable to type 'string'.
60+
!!! error TS2322: Type '{ [x: number]: undefined; }' is not assignable to type 'string'.
6161
i2.tuple1 = [{}];
6262
~~~~~~~~~
6363
!!! error TS2322: Type '[{}]' is not assignable to type '[{}, {}]'.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [inferentialTypingUsingApparentType1.ts]
2+
function foo<T extends (p: string) => number>(x: T): T {
3+
return undefined;
4+
}
5+
6+
foo(x => x.length);
7+
8+
//// [inferentialTypingUsingApparentType1.js]
9+
function foo(x) {
10+
return undefined;
11+
}
12+
foo(function (x) { return x.length; });
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/inferentialTypingUsingApparentType1.ts ===
2+
function foo<T extends (p: string) => number>(x: T): T {
3+
>foo : Symbol(foo, Decl(inferentialTypingUsingApparentType1.ts, 0, 0))
4+
>T : Symbol(T, Decl(inferentialTypingUsingApparentType1.ts, 0, 13))
5+
>p : Symbol(p, Decl(inferentialTypingUsingApparentType1.ts, 0, 24))
6+
>x : Symbol(x, Decl(inferentialTypingUsingApparentType1.ts, 0, 46))
7+
>T : Symbol(T, Decl(inferentialTypingUsingApparentType1.ts, 0, 13))
8+
>T : Symbol(T, Decl(inferentialTypingUsingApparentType1.ts, 0, 13))
9+
10+
return undefined;
11+
>undefined : Symbol(undefined)
12+
}
13+
14+
foo(x => x.length);
15+
>foo : Symbol(foo, Decl(inferentialTypingUsingApparentType1.ts, 0, 0))
16+
>x : Symbol(x, Decl(inferentialTypingUsingApparentType1.ts, 4, 4))
17+
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
18+
>x : Symbol(x, Decl(inferentialTypingUsingApparentType1.ts, 4, 4))
19+
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
20+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/inferentialTypingUsingApparentType1.ts ===
2+
function foo<T extends (p: string) => number>(x: T): T {
3+
>foo : <T extends (p: string) => number>(x: T) => T
4+
>T : T
5+
>p : string
6+
>x : T
7+
>T : T
8+
>T : T
9+
10+
return undefined;
11+
>undefined : undefined
12+
}
13+
14+
foo(x => x.length);
15+
>foo(x => x.length) : (x: string) => number
16+
>foo : <T extends (p: string) => number>(x: T) => T
17+
>x => x.length : (x: string) => number
18+
>x : string
19+
>x.length : number
20+
>x : string
21+
>length : number
22+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [inferentialTypingUsingApparentType2.ts]
2+
function foo<T extends { m(p: string): number }>(x: T): T {
3+
return undefined;
4+
}
5+
6+
foo({ m(x) { return x.length } });
7+
8+
//// [inferentialTypingUsingApparentType2.js]
9+
function foo(x) {
10+
return undefined;
11+
}
12+
foo({ m: function (x) { return x.length; } });

0 commit comments

Comments
 (0)