Skip to content

Commit 695aae7

Browse files
authored
Merge pull request #26848 from Microsoft/deferUnionIntersectionReduction
Defer union and intersection type reduction
2 parents f79e645 + d9e0d6b commit 695aae7

File tree

6 files changed

+26
-41
lines changed

6 files changed

+26
-41
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8720,10 +8720,7 @@ namespace ts {
87208720
const len = typeSet.length;
87218721
const index = len && type.id > typeSet[len - 1].id ? ~len : binarySearch(typeSet, type, getTypeId, compareValues);
87228722
if (index < 0) {
8723-
if (!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
8724-
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && containsIdenticalType(typeSet, type))) {
8725-
typeSet.splice(~index, 0, type);
8726-
}
8723+
typeSet.splice(~index, 0, type);
87278724
}
87288725
}
87298726
}
@@ -8739,15 +8736,6 @@ namespace ts {
87398736
return includes;
87408737
}
87418738

8742-
function containsIdenticalType(types: ReadonlyArray<Type>, type: Type) {
8743-
for (const t of types) {
8744-
if (isTypeIdenticalTo(t, type)) {
8745-
return true;
8746-
}
8747-
}
8748-
return false;
8749-
}
8750-
87518739
function isSubtypeOfAny(source: Type, targets: ReadonlyArray<Type>): boolean {
87528740
for (const target of targets) {
87538741
if (source !== target && isTypeSubtypeOf(source, target) && (
@@ -8928,10 +8916,7 @@ namespace ts {
89288916
if (flags & TypeFlags.AnyOrUnknown) {
89298917
if (type === wildcardType) includes |= TypeFlags.Wildcard;
89308918
}
8931-
else if ((strictNullChecks || !(flags & TypeFlags.Nullable)) && !contains(typeSet, type) &&
8932-
!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
8933-
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) &&
8934-
containsIdenticalType(typeSet, type))) {
8919+
else if ((strictNullChecks || !(flags & TypeFlags.Nullable)) && !contains(typeSet, type)) {
89358920
typeSet.push(type);
89368921
}
89378922
}

tests/baselines/reference/checkJsxChildrenProperty4.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/conformance/jsx/file.tsx(24,28): error TS2551: Property 'NAme' does not exist on type 'IUser'. Did you mean 'Name'?
2-
tests/cases/conformance/jsx/file.tsx(32,10): error TS2322: Type '{ children: ((user: IUser) => Element)[]; }' is not assignable to type 'IFetchUserProps'.
2+
tests/cases/conformance/jsx/file.tsx(32,10): error TS2322: Type '{ children: (((user: IUser) => Element) | ((user: IUser) => Element))[]; }' is not assignable to type 'IFetchUserProps'.
33
Types of property 'children' are incompatible.
4-
Type '((user: IUser) => Element)[]' is not assignable to type '(user: IUser) => Element'.
5-
Type '((user: IUser) => Element)[]' provides no match for the signature '(user: IUser): Element'.
4+
Type '(((user: IUser) => Element) | ((user: IUser) => Element))[]' is not assignable to type '(user: IUser) => Element'.
5+
Type '(((user: IUser) => Element) | ((user: IUser) => Element))[]' provides no match for the signature '(user: IUser): Element'.
66

77

88
==== tests/cases/conformance/jsx/file.tsx (2 errors) ====
@@ -42,10 +42,10 @@ tests/cases/conformance/jsx/file.tsx(32,10): error TS2322: Type '{ children: ((u
4242
return (
4343
<FetchUser>
4444
~~~~~~~~~
45-
!!! error TS2322: Type '{ children: ((user: IUser) => Element)[]; }' is not assignable to type 'IFetchUserProps'.
45+
!!! error TS2322: Type '{ children: (((user: IUser) => Element) | ((user: IUser) => Element))[]; }' is not assignable to type 'IFetchUserProps'.
4646
!!! error TS2322: Types of property 'children' are incompatible.
47-
!!! error TS2322: Type '((user: IUser) => Element)[]' is not assignable to type '(user: IUser) => Element'.
48-
!!! error TS2322: Type '((user: IUser) => Element)[]' provides no match for the signature '(user: IUser): Element'.
47+
!!! error TS2322: Type '(((user: IUser) => Element) | ((user: IUser) => Element))[]' is not assignable to type '(user: IUser) => Element'.
48+
!!! error TS2322: Type '(((user: IUser) => Element) | ((user: IUser) => Element))[]' provides no match for the signature '(user: IUser): Element'.
4949

5050

5151

tests/baselines/reference/overrideBaseIntersectionMethod.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ class Foo extends WithLocation(Point) {
7878

7979
return super.getLocation()
8080
>super.getLocation() : [number, number]
81-
>super.getLocation : () => [number, number]
81+
>super.getLocation : (() => [number, number]) & (() => [number, number])
8282
>super : WithLocation<typeof Point>.(Anonymous class) & Point
83-
>getLocation : () => [number, number]
83+
>getLocation : (() => [number, number]) & (() => [number, number])
8484
}
8585
whereAmI() {
8686
>whereAmI : () => [number, number]

tests/baselines/reference/typeParameterExtendingUnion1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ function f<T extends Cat | Dog>(a: T) {
3030

3131
a.run();
3232
>a.run() : void
33-
>a.run : () => void
33+
>a.run : (() => void) | (() => void)
3434
>a : T
35-
>run : () => void
35+
>run : (() => void) | (() => void)
3636

3737
run(a);
3838
>run(a) : void

tests/baselines/reference/typeParameterExtendingUnion2.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function run(a: Cat | Dog) {
1919

2020
a.run();
2121
>a.run() : void
22-
>a.run : () => void
22+
>a.run : (() => void) | (() => void)
2323
>a : Cat | Dog
24-
>run : () => void
24+
>run : (() => void) | (() => void)
2525
}
2626

2727
function f<T extends Cat | Dog>(a: T) {
@@ -30,9 +30,9 @@ function f<T extends Cat | Dog>(a: T) {
3030

3131
a.run();
3232
>a.run() : void
33-
>a.run : () => void
33+
>a.run : (() => void) | (() => void)
3434
>a : T
35-
>run : () => void
35+
>run : (() => void) | (() => void)
3636

3737
run(a);
3838
>run(a) : void

tests/baselines/reference/unionTypeMembers.types

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ str = x.commonMethodType(str); // (a: string) => string so result should be stri
9595
>str = x.commonMethodType(str) : string
9696
>str : string
9797
>x.commonMethodType(str) : string
98-
>x.commonMethodType : (a: string) => string
98+
>x.commonMethodType : ((a: string) => string) | ((a: string) => string)
9999
>x : I1<number> | I2<number>
100-
>commonMethodType : (a: string) => string
100+
>commonMethodType : ((a: string) => string) | ((a: string) => string)
101101
>str : string
102102

103103
strOrNum = x.commonPropertyDifferenType;
@@ -133,36 +133,36 @@ num = x.commonMethodWithTypeParameter(num);
133133
>num = x.commonMethodWithTypeParameter(num) : number
134134
>num : number
135135
>x.commonMethodWithTypeParameter(num) : number
136-
>x.commonMethodWithTypeParameter : (a: number) => number
136+
>x.commonMethodWithTypeParameter : ((a: number) => number) | ((a: number) => number)
137137
>x : I1<number> | I2<number>
138-
>commonMethodWithTypeParameter : (a: number) => number
138+
>commonMethodWithTypeParameter : ((a: number) => number) | ((a: number) => number)
139139
>num : number
140140

141141
num = x.commonMethodWithOwnTypeParameter(num);
142142
>num = x.commonMethodWithOwnTypeParameter(num) : number
143143
>num : number
144144
>x.commonMethodWithOwnTypeParameter(num) : number
145-
>x.commonMethodWithOwnTypeParameter : <U>(a: U) => U
145+
>x.commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
146146
>x : I1<number> | I2<number>
147-
>commonMethodWithOwnTypeParameter : <U>(a: U) => U
147+
>commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
148148
>num : number
149149

150150
str = x.commonMethodWithOwnTypeParameter(str);
151151
>str = x.commonMethodWithOwnTypeParameter(str) : string
152152
>str : string
153153
>x.commonMethodWithOwnTypeParameter(str) : string
154-
>x.commonMethodWithOwnTypeParameter : <U>(a: U) => U
154+
>x.commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
155155
>x : I1<number> | I2<number>
156-
>commonMethodWithOwnTypeParameter : <U>(a: U) => U
156+
>commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
157157
>str : string
158158

159159
strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum);
160160
>strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum) : string | number
161161
>strOrNum : string | number
162162
>x.commonMethodWithOwnTypeParameter(strOrNum) : string | number
163-
>x.commonMethodWithOwnTypeParameter : <U>(a: U) => U
163+
>x.commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
164164
>x : I1<number> | I2<number>
165-
>commonMethodWithOwnTypeParameter : <U>(a: U) => U
165+
>commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
166166
>strOrNum : string | number
167167

168168
x.propertyOnlyInI1; // error

0 commit comments

Comments
 (0)