Skip to content

Remove empty object types from intersection types #15033

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 3 commits into from
Apr 5, 2017
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
35 changes: 23 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6906,6 +6906,8 @@ namespace ts {
containsString?: boolean;
containsNumber?: boolean;
containsStringOrNumberLiteral?: boolean;
containsObjectType?: boolean;
containsEmptyObject?: boolean;
unionIndex?: number;
}

Expand Down Expand Up @@ -7101,7 +7103,13 @@ namespace ts {
else if (type.flags & TypeFlags.Any) {
typeSet.containsAny = true;
}
else if (getObjectFlags(type) & ObjectFlags.Anonymous && isEmptyObjectType(type)) {
typeSet.containsEmptyObject = true;
}
else if (!(type.flags & TypeFlags.Never) && (strictNullChecks || !(type.flags & TypeFlags.Nullable)) && !contains(typeSet, type)) {
if (type.flags & TypeFlags.Object) {
typeSet.containsObjectType = true;
}
if (type.flags & TypeFlags.Union && typeSet.unionIndex === undefined) {
typeSet.unionIndex = typeSet.length;
}
Expand Down Expand Up @@ -7139,6 +7147,9 @@ namespace ts {
if (typeSet.containsAny) {
return anyType;
}
if (typeSet.containsEmptyObject && !typeSet.containsObjectType) {
typeSet.push(emptyObjectType);
}
if (typeSet.length === 1) {
return typeSet[0];
}
Expand Down Expand Up @@ -8309,6 +8320,18 @@ namespace ts {
}
}

function isEmptyResolvedType(t: ResolvedType) {
return t.properties.length === 0 &&
t.callSignatures.length === 0 &&
t.constructSignatures.length === 0 &&
!t.stringIndexInfo &&
!t.numberIndexInfo;
}

function isEmptyObjectType(type: Type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we use (type===emptyObjectType) here? Do we really have to resolve every type and then check its structure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We create a fresh object type whenever an object literal is declared with an alias (such that we can associate a name with it). Likewise we create a fresh object type for each object literal expression (although we could potentially special case an empty object literal). Since these are all distinct object identities, we need to compare structure.

return type.flags & TypeFlags.Object && isEmptyResolvedType(resolveStructuredTypeMembers(<ObjectType>type));
}

function isEnumTypeRelatedTo(source: EnumType, target: EnumType, errorReporter?: ErrorReporter) {
if (source === target) {
return true;
Expand Down Expand Up @@ -8644,18 +8667,6 @@ namespace ts {
return false;
}

function isEmptyResolvedType(t: ResolvedType) {
return t.properties.length === 0 &&
t.callSignatures.length === 0 &&
t.constructSignatures.length === 0 &&
!t.stringIndexInfo &&
!t.numberIndexInfo;
}

function isEmptyObjectType(type: Type) {
return type.flags & TypeFlags.Object && isEmptyResolvedType(resolveStructuredTypeMembers(<ObjectType>type));
}

function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean {
if (maybeTypeOfKind(target, TypeFlags.Object) && !(getObjectFlags(target) & ObjectFlags.ObjectLiteralPatternWithComputedProperties)) {
const isComparingJsxAttributes = !!(source.flags & TypeFlags.JsxAttributes);
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/declarationEmitPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ exports.runSampleBreaks = runSampleBreaks;
export declare class bluebird<T> {
static all: Array<bluebird<any>>;
}
export declare function runSampleWorks<A, B, C, D, E>(a: bluebird<A>, b?: bluebird<B>, c?: bluebird<C>, d?: bluebird<D>, e?: bluebird<E>): Promise<(<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T) & {}>;
export declare function runSampleWorks<A, B, C, D, E>(a: bluebird<A>, b?: bluebird<B>, c?: bluebird<C>, d?: bluebird<D>, e?: bluebird<E>): Promise<(<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T)>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same as the next line? I'm a bit confused.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ends are different look closer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I was checking the js transpilation. (facepalm)

export declare function runSampleBreaks<A, B, C, D, E>(a: bluebird<A>, b?: bluebird<B>, c?: bluebird<C>, d?: bluebird<D>, e?: bluebird<E>): Promise<(<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T)>;
6 changes: 3 additions & 3 deletions tests/baselines/reference/declarationEmitPromise.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class bluebird<T> {
}

export async function runSampleWorks<A, B, C, D, E>(
>runSampleWorks : <A, B, C, D, E>(a: bluebird<A>, b?: bluebird<B>, c?: bluebird<C>, d?: bluebird<D>, e?: bluebird<E>) => Promise<(<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T) & {}>
>runSampleWorks : <A, B, C, D, E>(a: bluebird<A>, b?: bluebird<B>, c?: bluebird<C>, d?: bluebird<D>, e?: bluebird<E>) => Promise<(<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T)>
>A : A
>B : B
>C : C
Expand Down Expand Up @@ -85,13 +85,13 @@ export async function runSampleWorks<A, B, C, D, E>(
>result : any

let rfunc: typeof func & {} = func as any; // <- This is the only difference
>rfunc : (<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T) & {}
>rfunc : <T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T
>func : <T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T
>func as any : any
>func : <T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T

return rfunc
>rfunc : (<T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T) & {}
>rfunc : <T>(f: (a: A, b?: B, c?: C, d?: D, e?: E) => T) => T
}

export async function runSampleBreaks<A, B, C, D, E>(
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/genericDefaults.types
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ declare function f12<T, U = T & B>(a?: T, b?: U): [T, U];

// inference
f12();
>f12() : [{}, {} & B]
>f12() : [{}, B]
>f12 : <T, U = T & B>(a?: T, b?: U) => [T, U]

f12(a);
Expand Down Expand Up @@ -1856,7 +1856,7 @@ declare function f19<T = U, U = T & B>(a?: T, b?: U): [T, U];

// inference
f19();
>f19() : [{}, {} & B]
>f19() : [{}, B]
>f19 : <T = U, U = T & B>(a?: T, b?: U) => [T, U]

f19(a);
Expand Down Expand Up @@ -1950,11 +1950,11 @@ declare function f20<T, U = V, V = U & C>(a?: T, b?: U, c?: V): [T, U, V];

// inference
f20();
>f20() : [{}, {}, {} & C]
>f20() : [{}, {}, C]
>f20 : <T, U = V, V = U & C>(a?: T, b?: U, c?: V) => [T, U, V]

f20(a);
>f20(a) : [A, {}, {} & C]
>f20(a) : [A, {}, C]
>f20 : <T, U = V, V = U & C>(a?: T, b?: U, c?: V) => [T, U, V]
>a : A

Expand All @@ -1973,25 +1973,25 @@ f20(a, b, c);

// no inference, partially supplied
f20<A>();
>f20<A>() : [A, {}, {} & C]
>f20<A>() : [A, {}, C]
>f20 : <T, U = V, V = U & C>(a?: T, b?: U, c?: V) => [T, U, V]
>A : A

f20<A>(a);
>f20<A>(a) : [A, {}, {} & C]
>f20<A>(a) : [A, {}, C]
>f20 : <T, U = V, V = U & C>(a?: T, b?: U, c?: V) => [T, U, V]
>A : A
>a : A

f20<A>(a, b);
>f20<A>(a, b) : [A, {}, {} & C]
>f20<A>(a, b) : [A, {}, C]
>f20 : <T, U = V, V = U & C>(a?: T, b?: U, c?: V) => [T, U, V]
>A : A
>a : A
>b : B

f20<A>(a, b, bc);
>f20<A>(a, b, bc) : [A, {}, {} & C]
>f20<A>(a, b, bc) : [A, {}, C]
>f20 : <T, U = V, V = U & C>(a?: T, b?: U, c?: V) => [T, U, V]
>A : A
>a : A
Expand Down
45 changes: 45 additions & 0 deletions tests/baselines/reference/intersectionsAndEmptyObjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [intersectionsAndEmptyObjects.ts]
// Empty object type literals are removed from intersections types
// that contain other object types

type A = { a: number };
type B = { b: string };
type C = {};

let x01: A & B;
let x02: A & C;
let x03: B & C;
let x04: A & B & C;
let x05: string & C;
let x06: C & string;
let x07: C;
let x08: C & {};
let x09: {} & A & {} & B & {} & C & {};

interface D {}
interface E {}

let x10: A & D;
let x11: C & D;
let x12: A & B & C & D;
let x13: D & E;
let x14: A & B & C & D & E;


//// [intersectionsAndEmptyObjects.js]
// Empty object type literals are removed from intersections types
// that contain other object types
var x01;
var x02;
var x03;
var x04;
var x05;
var x06;
var x07;
var x08;
var x09;
var x10;
var x11;
var x12;
var x13;
var x14;
94 changes: 94 additions & 0 deletions tests/baselines/reference/intersectionsAndEmptyObjects.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
=== tests/cases/conformance/types/intersection/intersectionsAndEmptyObjects.ts ===
// Empty object type literals are removed from intersections types
// that contain other object types

type A = { a: number };
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>a : Symbol(a, Decl(intersectionsAndEmptyObjects.ts, 3, 10))

type B = { b: string };
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))
>b : Symbol(b, Decl(intersectionsAndEmptyObjects.ts, 4, 10))

type C = {};
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x01: A & B;
>x01 : Symbol(x01, Decl(intersectionsAndEmptyObjects.ts, 7, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))

let x02: A & C;
>x02 : Symbol(x02, Decl(intersectionsAndEmptyObjects.ts, 8, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x03: B & C;
>x03 : Symbol(x03, Decl(intersectionsAndEmptyObjects.ts, 9, 3))
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x04: A & B & C;
>x04 : Symbol(x04, Decl(intersectionsAndEmptyObjects.ts, 10, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x05: string & C;
>x05 : Symbol(x05, Decl(intersectionsAndEmptyObjects.ts, 11, 3))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x06: C & string;
>x06 : Symbol(x06, Decl(intersectionsAndEmptyObjects.ts, 12, 3))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x07: C;
>x07 : Symbol(x07, Decl(intersectionsAndEmptyObjects.ts, 13, 3))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x08: C & {};
>x08 : Symbol(x08, Decl(intersectionsAndEmptyObjects.ts, 14, 3))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

let x09: {} & A & {} & B & {} & C & {};
>x09 : Symbol(x09, Decl(intersectionsAndEmptyObjects.ts, 15, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))

interface D {}
>D : Symbol(D, Decl(intersectionsAndEmptyObjects.ts, 15, 39))

interface E {}
>E : Symbol(E, Decl(intersectionsAndEmptyObjects.ts, 17, 14))

let x10: A & D;
>x10 : Symbol(x10, Decl(intersectionsAndEmptyObjects.ts, 20, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>D : Symbol(D, Decl(intersectionsAndEmptyObjects.ts, 15, 39))

let x11: C & D;
>x11 : Symbol(x11, Decl(intersectionsAndEmptyObjects.ts, 21, 3))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))
>D : Symbol(D, Decl(intersectionsAndEmptyObjects.ts, 15, 39))

let x12: A & B & C & D;
>x12 : Symbol(x12, Decl(intersectionsAndEmptyObjects.ts, 22, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))
>D : Symbol(D, Decl(intersectionsAndEmptyObjects.ts, 15, 39))

let x13: D & E;
>x13 : Symbol(x13, Decl(intersectionsAndEmptyObjects.ts, 23, 3))
>D : Symbol(D, Decl(intersectionsAndEmptyObjects.ts, 15, 39))
>E : Symbol(E, Decl(intersectionsAndEmptyObjects.ts, 17, 14))

let x14: A & B & C & D & E;
>x14 : Symbol(x14, Decl(intersectionsAndEmptyObjects.ts, 24, 3))
>A : Symbol(A, Decl(intersectionsAndEmptyObjects.ts, 0, 0))
>B : Symbol(B, Decl(intersectionsAndEmptyObjects.ts, 3, 23))
>C : Symbol(C, Decl(intersectionsAndEmptyObjects.ts, 4, 23))
>D : Symbol(D, Decl(intersectionsAndEmptyObjects.ts, 15, 39))
>E : Symbol(E, Decl(intersectionsAndEmptyObjects.ts, 17, 14))

Loading