Skip to content

Commit be409fa

Browse files
authored
Merge pull request #31137 from Microsoft/fixConditionalInference
Fix conditional type inference involving any or unknown
2 parents a539887 + 31551fd commit be409fa

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10386,11 +10386,11 @@ namespace ts {
1038610386
// We attempt to resolve the conditional type only when the check and extends types are non-generic
1038710387
if (!checkTypeInstantiable && !maybeTypeOfKind(inferredExtendsType, TypeFlags.Instantiable | TypeFlags.GenericMappedType)) {
1038810388
if (inferredExtendsType.flags & TypeFlags.AnyOrUnknown) {
10389-
return trueType;
10389+
return combinedMapper ? instantiateType(root.trueType, combinedMapper) : trueType;
1039010390
}
1039110391
// Return union of trueType and falseType for 'any' since it matches anything
1039210392
if (checkType.flags & TypeFlags.Any) {
10393-
return getUnionType([instantiateType(root.trueType, combinedMapper || mapper), falseType]);
10393+
return getUnionType([combinedMapper ? instantiateType(root.trueType, combinedMapper) : trueType, falseType]);
1039410394
}
1039510395
// Return falseType for a definitely false extends check. We check an instantiations of the two
1039610396
// types with type parameters mapped to the wildcard type, the most permissive instantiations
@@ -10405,7 +10405,7 @@ namespace ts {
1040510405
// type Foo<T extends { x: any }> = T extends { x: string } ? string : number
1040610406
// doesn't immediately resolve to 'string' instead of being deferred.
1040710407
if (isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(inferredExtendsType))) {
10408-
return instantiateType(root.trueType, combinedMapper || mapper);
10408+
return combinedMapper ? instantiateType(root.trueType, combinedMapper) : trueType;
1040910409
}
1041010410
}
1041110411
// Return a deferred type for a check that is neither definitely true nor definitely false

tests/baselines/reference/inferTypes2.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ export declare function foo2<T>(obj: T): T extends { [K in keyof BadNested<infer
1212
export function bar2<T>(obj: T) {
1313
return foo2(obj);
1414
}
15+
16+
// Repros from #31099
17+
18+
type Weird = any extends infer U ? U : never;
19+
type AlsoWeird = unknown extends infer U ? U : never;
20+
21+
const a: Weird = null;
22+
const b: string = a;
1523

1624

1725
//// [inferTypes2.js]
@@ -26,6 +34,8 @@ function bar2(obj) {
2634
return foo2(obj);
2735
}
2836
exports.bar2 = bar2;
37+
var a = null;
38+
var b = a;
2939

3040

3141
//// [inferTypes2.d.ts]

tests/baselines/reference/inferTypes2.symbols

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@ export function bar2<T>(obj: T) {
5353
>obj : Symbol(obj, Decl(inferTypes2.ts, 10, 24))
5454
}
5555

56+
// Repros from #31099
57+
58+
type Weird = any extends infer U ? U : never;
59+
>Weird : Symbol(Weird, Decl(inferTypes2.ts, 12, 1))
60+
>U : Symbol(U, Decl(inferTypes2.ts, 16, 30))
61+
>U : Symbol(U, Decl(inferTypes2.ts, 16, 30))
62+
63+
type AlsoWeird = unknown extends infer U ? U : never;
64+
>AlsoWeird : Symbol(AlsoWeird, Decl(inferTypes2.ts, 16, 45))
65+
>U : Symbol(U, Decl(inferTypes2.ts, 17, 38))
66+
>U : Symbol(U, Decl(inferTypes2.ts, 17, 38))
67+
68+
const a: Weird = null;
69+
>a : Symbol(a, Decl(inferTypes2.ts, 19, 5))
70+
>Weird : Symbol(Weird, Decl(inferTypes2.ts, 12, 1))
71+
72+
const b: string = a;
73+
>b : Symbol(b, Decl(inferTypes2.ts, 20, 5))
74+
>a : Symbol(a, Decl(inferTypes2.ts, 19, 5))
75+

tests/baselines/reference/inferTypes2.types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ export function bar2<T>(obj: T) {
3333
>obj : T
3434
}
3535

36+
// Repros from #31099
37+
38+
type Weird = any extends infer U ? U : never;
39+
>Weird : any
40+
41+
type AlsoWeird = unknown extends infer U ? U : never;
42+
>AlsoWeird : unknown
43+
44+
const a: Weird = null;
45+
>a : any
46+
>null : null
47+
48+
const b: string = a;
49+
>b : string
50+
>a : any
51+

tests/cases/conformance/types/conditional/inferTypes2.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ export declare function foo2<T>(obj: T): T extends { [K in keyof BadNested<infer
1414
export function bar2<T>(obj: T) {
1515
return foo2(obj);
1616
}
17+
18+
// Repros from #31099
19+
20+
type Weird = any extends infer U ? U : never;
21+
type AlsoWeird = unknown extends infer U ? U : never;
22+
23+
const a: Weird = null;
24+
const b: string = a;

0 commit comments

Comments
 (0)