Skip to content

Commit 4574c7a

Browse files
authored
Merge pull request #30877 from Microsoft/fixConditionalTypeSimplification
Fix conditional type simplification
2 parents 6282645 + 06d07b0 commit 4574c7a

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10250,15 +10250,15 @@ namespace ts {
1025010250

1025110251
function getConditionalTypeWorker(root: ConditionalRoot, mapper: TypeMapper | undefined, checkType: Type, extendsType: Type, trueType: Type, falseType: Type) {
1025210252
// Simplifications for types of the form `T extends U ? T : never` and `T extends U ? never : T`.
10253-
if (falseType.flags & TypeFlags.Never && isTypeIdenticalTo(getActualTypeVariable(trueType), getActualTypeVariable(checkType))) {
10253+
if (falseType.flags & TypeFlags.Never && getActualTypeVariable(trueType) === getActualTypeVariable(checkType)) {
1025410254
if (checkType.flags & TypeFlags.Any || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
1025510255
return trueType;
1025610256
}
1025710257
else if (isIntersectionEmpty(checkType, extendsType)) { // Always false
1025810258
return neverType;
1025910259
}
1026010260
}
10261-
else if (trueType.flags & TypeFlags.Never && isTypeIdenticalTo(getActualTypeVariable(falseType), getActualTypeVariable(checkType))) {
10261+
else if (trueType.flags & TypeFlags.Never && getActualTypeVariable(falseType) === getActualTypeVariable(checkType)) {
1026210262
if (!(checkType.flags & TypeFlags.Any) && isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
1026310263
return neverType;
1026410264
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [conditionalTypeSimplification.ts]
2+
// Repro from #30794
3+
4+
interface AbstractSchema<S, V> {
5+
m1<T> (v: T): SchemaType<S, Exclude<V, T>>;
6+
m2<T> (v: T): SchemaType<S, T>;
7+
}
8+
9+
type SchemaType<S, V> = S extends object ? AnySchema<V> : never;
10+
interface AnySchema<V> extends AnySchemaType<AnySchema<undefined>, V> { }
11+
interface AnySchemaType<S extends AbstractSchema<any, any>, V> extends AbstractSchema<S, V> { }
12+
13+
14+
//// [conditionalTypeSimplification.js]
15+
// Repro from #30794
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=== tests/cases/compiler/conditionalTypeSimplification.ts ===
2+
// Repro from #30794
3+
4+
interface AbstractSchema<S, V> {
5+
>AbstractSchema : Symbol(AbstractSchema, Decl(conditionalTypeSimplification.ts, 0, 0))
6+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 2, 25))
7+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 2, 27))
8+
9+
m1<T> (v: T): SchemaType<S, Exclude<V, T>>;
10+
>m1 : Symbol(AbstractSchema.m1, Decl(conditionalTypeSimplification.ts, 2, 32))
11+
>T : Symbol(T, Decl(conditionalTypeSimplification.ts, 3, 5))
12+
>v : Symbol(v, Decl(conditionalTypeSimplification.ts, 3, 9))
13+
>T : Symbol(T, Decl(conditionalTypeSimplification.ts, 3, 5))
14+
>SchemaType : Symbol(SchemaType, Decl(conditionalTypeSimplification.ts, 5, 1))
15+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 2, 25))
16+
>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --))
17+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 2, 27))
18+
>T : Symbol(T, Decl(conditionalTypeSimplification.ts, 3, 5))
19+
20+
m2<T> (v: T): SchemaType<S, T>;
21+
>m2 : Symbol(AbstractSchema.m2, Decl(conditionalTypeSimplification.ts, 3, 45))
22+
>T : Symbol(T, Decl(conditionalTypeSimplification.ts, 4, 5))
23+
>v : Symbol(v, Decl(conditionalTypeSimplification.ts, 4, 9))
24+
>T : Symbol(T, Decl(conditionalTypeSimplification.ts, 4, 5))
25+
>SchemaType : Symbol(SchemaType, Decl(conditionalTypeSimplification.ts, 5, 1))
26+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 2, 25))
27+
>T : Symbol(T, Decl(conditionalTypeSimplification.ts, 4, 5))
28+
}
29+
30+
type SchemaType<S, V> = S extends object ? AnySchema<V> : never;
31+
>SchemaType : Symbol(SchemaType, Decl(conditionalTypeSimplification.ts, 5, 1))
32+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 7, 16))
33+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 7, 18))
34+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 7, 16))
35+
>AnySchema : Symbol(AnySchema, Decl(conditionalTypeSimplification.ts, 7, 64))
36+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 7, 18))
37+
38+
interface AnySchema<V> extends AnySchemaType<AnySchema<undefined>, V> { }
39+
>AnySchema : Symbol(AnySchema, Decl(conditionalTypeSimplification.ts, 7, 64))
40+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 8, 20))
41+
>AnySchemaType : Symbol(AnySchemaType, Decl(conditionalTypeSimplification.ts, 8, 73))
42+
>AnySchema : Symbol(AnySchema, Decl(conditionalTypeSimplification.ts, 7, 64))
43+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 8, 20))
44+
45+
interface AnySchemaType<S extends AbstractSchema<any, any>, V> extends AbstractSchema<S, V> { }
46+
>AnySchemaType : Symbol(AnySchemaType, Decl(conditionalTypeSimplification.ts, 8, 73))
47+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 9, 24))
48+
>AbstractSchema : Symbol(AbstractSchema, Decl(conditionalTypeSimplification.ts, 0, 0))
49+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 9, 59))
50+
>AbstractSchema : Symbol(AbstractSchema, Decl(conditionalTypeSimplification.ts, 0, 0))
51+
>S : Symbol(S, Decl(conditionalTypeSimplification.ts, 9, 24))
52+
>V : Symbol(V, Decl(conditionalTypeSimplification.ts, 9, 59))
53+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/conditionalTypeSimplification.ts ===
2+
// Repro from #30794
3+
4+
interface AbstractSchema<S, V> {
5+
m1<T> (v: T): SchemaType<S, Exclude<V, T>>;
6+
>m1 : <T>(v: T) => SchemaType<S, Exclude<V, T>>
7+
>v : T
8+
9+
m2<T> (v: T): SchemaType<S, T>;
10+
>m2 : <T>(v: T) => SchemaType<S, T>
11+
>v : T
12+
}
13+
14+
type SchemaType<S, V> = S extends object ? AnySchema<V> : never;
15+
>SchemaType : SchemaType<S, V>
16+
17+
interface AnySchema<V> extends AnySchemaType<AnySchema<undefined>, V> { }
18+
interface AnySchemaType<S extends AbstractSchema<any, any>, V> extends AbstractSchema<S, V> { }
19+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Repro from #30794
2+
3+
interface AbstractSchema<S, V> {
4+
m1<T> (v: T): SchemaType<S, Exclude<V, T>>;
5+
m2<T> (v: T): SchemaType<S, T>;
6+
}
7+
8+
type SchemaType<S, V> = S extends object ? AnySchema<V> : never;
9+
interface AnySchema<V> extends AnySchemaType<AnySchema<undefined>, V> { }
10+
interface AnySchemaType<S extends AbstractSchema<any, any>, V> extends AbstractSchema<S, V> { }

0 commit comments

Comments
 (0)