Skip to content

Commit ec8f425

Browse files
committed
tweak instantiateContextualType further
1 parent c0ea121 commit ec8f425

File tree

4 files changed

+277
-3
lines changed

4 files changed

+277
-3
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32652,9 +32652,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3265232652
// the 'boolean' type from the contextual type such that contextually typed boolean
3265332653
// literals actually end up widening to 'boolean' (see #48363).
3265432654
const type = instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
32655-
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
32656-
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
32657-
type;
32655+
if (!(type.flags & TypeFlags.AnyOrUnknown)) {
32656+
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
32657+
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
32658+
type;
32659+
}
3265832660
}
3265932661
}
3266032662
return contextualType;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//// [tests/cases/compiler/returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts] ////
2+
3+
=== returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts ===
4+
declare function outer1(arg: { prop: any }): void;
5+
>outer1 : Symbol(outer1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 0))
6+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 24))
7+
>prop : Symbol(prop, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 30))
8+
9+
declare function outer2(arg: { prop: unknown }): void;
10+
>outer2 : Symbol(outer2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 50))
11+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 1, 24))
12+
>prop : Symbol(prop, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 1, 30))
13+
14+
declare function inner1<T extends (arg: string) => any>(arg: T): T;
15+
>inner1 : Symbol(inner1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 1, 54))
16+
>T : Symbol(T, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 3, 24))
17+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 3, 35))
18+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 3, 56))
19+
>T : Symbol(T, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 3, 24))
20+
>T : Symbol(T, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 3, 24))
21+
22+
const result1 = inner1((arg) => arg);
23+
>result1 : Symbol(result1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 5, 5))
24+
>inner1 : Symbol(inner1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 1, 54))
25+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 5, 24))
26+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 5, 24))
27+
28+
outer1({
29+
>outer1 : Symbol(outer1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 0))
30+
31+
prop: inner1((arg) => arg),
32+
>prop : Symbol(prop, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 7, 8))
33+
>inner1 : Symbol(inner1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 1, 54))
34+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 8, 16))
35+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 8, 16))
36+
37+
});
38+
39+
outer2({
40+
>outer2 : Symbol(outer2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 50))
41+
42+
prop: inner1((arg) => arg),
43+
>prop : Symbol(prop, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 11, 8))
44+
>inner1 : Symbol(inner1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 1, 54))
45+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 12, 16))
46+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 12, 16))
47+
48+
});
49+
50+
declare function inner2<T>(arg: T & ((arg: string) => any)): T;
51+
>inner2 : Symbol(inner2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 13, 3))
52+
>T : Symbol(T, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 15, 24))
53+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 15, 27))
54+
>T : Symbol(T, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 15, 24))
55+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 15, 38))
56+
>T : Symbol(T, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 15, 24))
57+
58+
const result2 = inner2((arg) => arg);
59+
>result2 : Symbol(result2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 17, 5))
60+
>inner2 : Symbol(inner2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 13, 3))
61+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 17, 24))
62+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 17, 24))
63+
64+
outer1({
65+
>outer1 : Symbol(outer1, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 0))
66+
67+
prop: inner2((arg) => arg),
68+
>prop : Symbol(prop, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 19, 8))
69+
>inner2 : Symbol(inner2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 13, 3))
70+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 20, 16))
71+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 20, 16))
72+
73+
});
74+
75+
outer2({
76+
>outer2 : Symbol(outer2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 0, 50))
77+
78+
prop: inner2((arg) => arg),
79+
>prop : Symbol(prop, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 23, 8))
80+
>inner2 : Symbol(inner2, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 13, 3))
81+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 24, 16))
82+
>arg : Symbol(arg, Decl(returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts, 24, 16))
83+
84+
});
85+
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
//// [tests/cases/compiler/returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts] ////
2+
3+
=== returnTypeInferenceContextualTypeIgnoreAnyUnknown1.ts ===
4+
declare function outer1(arg: { prop: any }): void;
5+
>outer1 : (arg: { prop: any; }) => void
6+
> : ^ ^^ ^^^^^
7+
>arg : { prop: any; }
8+
> : ^^^^^^^^ ^^^
9+
>prop : any
10+
11+
declare function outer2(arg: { prop: unknown }): void;
12+
>outer2 : (arg: { prop: unknown; }) => void
13+
> : ^ ^^ ^^^^^
14+
>arg : { prop: unknown; }
15+
> : ^^^^^^^^ ^^^
16+
>prop : unknown
17+
> : ^^^^^^^
18+
19+
declare function inner1<T extends (arg: string) => any>(arg: T): T;
20+
>inner1 : <T extends (arg: string) => any>(arg: T) => T
21+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
22+
>arg : string
23+
> : ^^^^^^
24+
>arg : T
25+
> : ^
26+
27+
const result1 = inner1((arg) => arg);
28+
>result1 : (arg: string) => string
29+
> : ^ ^^^^^^^^^^^^^^^^^^^
30+
>inner1((arg) => arg) : (arg: string) => string
31+
> : ^ ^^^^^^^^^^^^^^^^^^^
32+
>inner1 : <T extends (arg: string) => any>(arg: T) => T
33+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
34+
>(arg) => arg : (arg: string) => string
35+
> : ^ ^^^^^^^^^^^^^^^^^^^
36+
>arg : string
37+
> : ^^^^^^
38+
>arg : string
39+
> : ^^^^^^
40+
41+
outer1({
42+
>outer1({ prop: inner1((arg) => arg),}) : void
43+
> : ^^^^
44+
>outer1 : (arg: { prop: any; }) => void
45+
> : ^ ^^ ^^^^^
46+
>{ prop: inner1((arg) => arg),} : { prop: (arg: string) => string; }
47+
> : ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
48+
49+
prop: inner1((arg) => arg),
50+
>prop : (arg: string) => string
51+
> : ^ ^^^^^^^^^^^^^^^^^^^
52+
>inner1((arg) => arg) : (arg: string) => string
53+
> : ^ ^^^^^^^^^^^^^^^^^^^
54+
>inner1 : <T extends (arg: string) => any>(arg: T) => T
55+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
56+
>(arg) => arg : (arg: string) => string
57+
> : ^ ^^^^^^^^^^^^^^^^^^^
58+
>arg : string
59+
> : ^^^^^^
60+
>arg : string
61+
> : ^^^^^^
62+
63+
});
64+
65+
outer2({
66+
>outer2({ prop: inner1((arg) => arg),}) : void
67+
> : ^^^^
68+
>outer2 : (arg: { prop: unknown; }) => void
69+
> : ^ ^^ ^^^^^
70+
>{ prop: inner1((arg) => arg),} : { prop: (arg: string) => string; }
71+
> : ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
72+
73+
prop: inner1((arg) => arg),
74+
>prop : (arg: string) => string
75+
> : ^ ^^^^^^^^^^^^^^^^^^^
76+
>inner1((arg) => arg) : (arg: string) => string
77+
> : ^ ^^^^^^^^^^^^^^^^^^^
78+
>inner1 : <T extends (arg: string) => any>(arg: T) => T
79+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
80+
>(arg) => arg : (arg: string) => string
81+
> : ^ ^^^^^^^^^^^^^^^^^^^
82+
>arg : string
83+
> : ^^^^^^
84+
>arg : string
85+
> : ^^^^^^
86+
87+
});
88+
89+
declare function inner2<T>(arg: T & ((arg: string) => any)): T;
90+
>inner2 : <T>(arg: T & ((arg: string) => any)) => T
91+
> : ^ ^^ ^^ ^^^^^
92+
>arg : T & ((arg: string) => any)
93+
> : ^^^^^^ ^^ ^^^^^ ^
94+
>arg : string
95+
> : ^^^^^^
96+
97+
const result2 = inner2((arg) => arg);
98+
>result2 : (arg: string) => string
99+
> : ^ ^^^^^^^^^^^^^^^^^^^
100+
>inner2((arg) => arg) : (arg: string) => string
101+
> : ^ ^^^^^^^^^^^^^^^^^^^
102+
>inner2 : <T>(arg: T & ((arg: string) => any)) => T
103+
> : ^ ^^ ^^ ^^^^^
104+
>(arg) => arg : (arg: string) => string
105+
> : ^ ^^^^^^^^^^^^^^^^^^^
106+
>arg : string
107+
> : ^^^^^^
108+
>arg : string
109+
> : ^^^^^^
110+
111+
outer1({
112+
>outer1({ prop: inner2((arg) => arg),}) : void
113+
> : ^^^^
114+
>outer1 : (arg: { prop: any; }) => void
115+
> : ^ ^^ ^^^^^
116+
>{ prop: inner2((arg) => arg),} : { prop: (arg: string) => string; }
117+
> : ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
118+
119+
prop: inner2((arg) => arg),
120+
>prop : (arg: string) => string
121+
> : ^ ^^^^^^^^^^^^^^^^^^^
122+
>inner2((arg) => arg) : (arg: string) => string
123+
> : ^ ^^^^^^^^^^^^^^^^^^^
124+
>inner2 : <T>(arg: T & ((arg: string) => any)) => T
125+
> : ^ ^^ ^^ ^^^^^
126+
>(arg) => arg : (arg: string) => string
127+
> : ^ ^^^^^^^^^^^^^^^^^^^
128+
>arg : string
129+
> : ^^^^^^
130+
>arg : string
131+
> : ^^^^^^
132+
133+
});
134+
135+
outer2({
136+
>outer2({ prop: inner2((arg) => arg),}) : void
137+
> : ^^^^
138+
>outer2 : (arg: { prop: unknown; }) => void
139+
> : ^ ^^ ^^^^^
140+
>{ prop: inner2((arg) => arg),} : { prop: (arg: string) => string; }
141+
> : ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
142+
143+
prop: inner2((arg) => arg),
144+
>prop : (arg: string) => string
145+
> : ^ ^^^^^^^^^^^^^^^^^^^
146+
>inner2((arg) => arg) : (arg: string) => string
147+
> : ^ ^^^^^^^^^^^^^^^^^^^
148+
>inner2 : <T>(arg: T & ((arg: string) => any)) => T
149+
> : ^ ^^ ^^ ^^^^^
150+
>(arg) => arg : (arg: string) => string
151+
> : ^ ^^^^^^^^^^^^^^^^^^^
152+
>arg : string
153+
> : ^^^^^^
154+
>arg : string
155+
> : ^^^^^^
156+
157+
});
158+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
declare function outer1(arg: { prop: any }): void;
5+
declare function outer2(arg: { prop: unknown }): void;
6+
7+
declare function inner1<T extends (arg: string) => any>(arg: T): T;
8+
9+
const result1 = inner1((arg) => arg);
10+
11+
outer1({
12+
prop: inner1((arg) => arg),
13+
});
14+
15+
outer2({
16+
prop: inner1((arg) => arg),
17+
});
18+
19+
declare function inner2<T>(arg: T & ((arg: string) => any)): T;
20+
21+
const result2 = inner2((arg) => arg);
22+
23+
outer1({
24+
prop: inner2((arg) => arg),
25+
});
26+
27+
outer2({
28+
prop: inner2((arg) => arg),
29+
});

0 commit comments

Comments
 (0)