Skip to content

Defer union and intersection type reduction #26848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8691,10 +8691,7 @@ namespace ts {
const len = typeSet.length;
const index = len && type.id > typeSet[len - 1].id ? ~len : binarySearch(typeSet, type, getTypeId, compareValues);
if (index < 0) {
if (!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && containsIdenticalType(typeSet, type))) {
typeSet.splice(~index, 0, type);
}
typeSet.splice(~index, 0, type);
}
}
}
Expand All @@ -8710,15 +8707,6 @@ namespace ts {
return includes;
}

function containsIdenticalType(types: ReadonlyArray<Type>, type: Type) {
for (const t of types) {
if (isTypeIdenticalTo(t, type)) {
return true;
}
}
return false;
}

function isSubtypeOfAny(source: Type, targets: ReadonlyArray<Type>): boolean {
for (const target of targets) {
if (source !== target && isTypeSubtypeOf(source, target) && (
Expand Down Expand Up @@ -8899,10 +8887,7 @@ namespace ts {
if (flags & TypeFlags.AnyOrUnknown) {
if (type === wildcardType) includes |= TypeFlags.Wildcard;
}
else if ((strictNullChecks || !(flags & TypeFlags.Nullable)) && !contains(typeSet, type) &&
!(flags & TypeFlags.Object && (<ObjectType>type).objectFlags & ObjectFlags.Anonymous &&
type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) &&
containsIdenticalType(typeSet, type))) {
else if ((strictNullChecks || !(flags & TypeFlags.Nullable)) && !contains(typeSet, type)) {
typeSet.push(type);
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/checkJsxChildrenProperty4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/jsx/file.tsx(24,28): error TS2551: Property 'NAme' does not exist on type 'IUser'. Did you mean 'Name'?
tests/cases/conformance/jsx/file.tsx(32,10): error TS2322: Type '{ children: ((user: IUser) => Element)[]; }' is not assignable to type 'IFetchUserProps'.
tests/cases/conformance/jsx/file.tsx(32,10): error TS2322: Type '{ children: (((user: IUser) => Element) | ((user: IUser) => Element))[]; }' is not assignable to type 'IFetchUserProps'.
Types of property 'children' are incompatible.
Type '((user: IUser) => Element)[]' is not assignable to type '(user: IUser) => Element'.
Type '((user: IUser) => Element)[]' provides no match for the signature '(user: IUser): Element'.
Type '(((user: IUser) => Element) | ((user: IUser) => Element))[]' is not assignable to type '(user: IUser) => Element'.
Type '(((user: IUser) => Element) | ((user: IUser) => Element))[]' provides no match for the signature '(user: IUser): Element'.


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



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class Foo extends WithLocation(Point) {

return super.getLocation()
>super.getLocation() : [number, number]
>super.getLocation : () => [number, number]
>super.getLocation : (() => [number, number]) & (() => [number, number])
>super : WithLocation<typeof Point>.(Anonymous class) & Point
>getLocation : () => [number, number]
>getLocation : (() => [number, number]) & (() => [number, number])
}
whereAmI() {
>whereAmI : () => [number, number]
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/typeParameterExtendingUnion1.types
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ function f<T extends Cat | Dog>(a: T) {

a.run();
>a.run() : void
>a.run : () => void
>a.run : (() => void) | (() => void)
>a : T
>run : () => void
>run : (() => void) | (() => void)

run(a);
>run(a) : void
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/typeParameterExtendingUnion2.types
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function run(a: Cat | Dog) {

a.run();
>a.run() : void
>a.run : () => void
>a.run : (() => void) | (() => void)
>a : Cat | Dog
>run : () => void
>run : (() => void) | (() => void)
}

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

a.run();
>a.run() : void
>a.run : () => void
>a.run : (() => void) | (() => void)
>a : T
>run : () => void
>run : (() => void) | (() => void)

run(a);
>run(a) : void
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/unionTypeMembers.types
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ str = x.commonMethodType(str); // (a: string) => string so result should be stri
>str = x.commonMethodType(str) : string
>str : string
>x.commonMethodType(str) : string
>x.commonMethodType : (a: string) => string
>x.commonMethodType : ((a: string) => string) | ((a: string) => string)
>x : I1<number> | I2<number>
>commonMethodType : (a: string) => string
>commonMethodType : ((a: string) => string) | ((a: string) => string)
>str : string

strOrNum = x.commonPropertyDifferenType;
Expand Down Expand Up @@ -133,36 +133,36 @@ num = x.commonMethodWithTypeParameter(num);
>num = x.commonMethodWithTypeParameter(num) : number
>num : number
>x.commonMethodWithTypeParameter(num) : number
>x.commonMethodWithTypeParameter : (a: number) => number
>x.commonMethodWithTypeParameter : ((a: number) => number) | ((a: number) => number)
>x : I1<number> | I2<number>
>commonMethodWithTypeParameter : (a: number) => number
>commonMethodWithTypeParameter : ((a: number) => number) | ((a: number) => number)
>num : number

num = x.commonMethodWithOwnTypeParameter(num);
>num = x.commonMethodWithOwnTypeParameter(num) : number
>num : number
>x.commonMethodWithOwnTypeParameter(num) : number
>x.commonMethodWithOwnTypeParameter : <U>(a: U) => U
>x.commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
>x : I1<number> | I2<number>
>commonMethodWithOwnTypeParameter : <U>(a: U) => U
>commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
>num : number

str = x.commonMethodWithOwnTypeParameter(str);
>str = x.commonMethodWithOwnTypeParameter(str) : string
>str : string
>x.commonMethodWithOwnTypeParameter(str) : string
>x.commonMethodWithOwnTypeParameter : <U>(a: U) => U
>x.commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
>x : I1<number> | I2<number>
>commonMethodWithOwnTypeParameter : <U>(a: U) => U
>commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
>str : string

strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum);
>strOrNum = x.commonMethodWithOwnTypeParameter(strOrNum) : string | number
>strOrNum : string | number
>x.commonMethodWithOwnTypeParameter(strOrNum) : string | number
>x.commonMethodWithOwnTypeParameter : <U>(a: U) => U
>x.commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
>x : I1<number> | I2<number>
>commonMethodWithOwnTypeParameter : <U>(a: U) => U
>commonMethodWithOwnTypeParameter : (<U>(a: U) => U) | (<U>(a: U) => U)
>strOrNum : string | number

x.propertyOnlyInI1; // error
Expand Down