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

Conversation

ahejlsberg
Copy link
Member

With this PR we remove empty object type literals from intersection types that already contain other object types. For example, the intersection { x: number } & {} is reduced to just { x: number }.

Fixes #14762.

@ahejlsberg ahejlsberg changed the title Empty object intersection Remove empty object types from intersection types Apr 5, 2017
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Just one question.

As future work, it would be nice to combine all anonymous types together, eg D & { a: number } & { b: string } & { } should become D & { a: number, b: string }

!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.

@ahejlsberg ahejlsberg merged commit dce7fca into master Apr 5, 2017
@ahejlsberg ahejlsberg deleted the emptyObjectIntersection branch April 5, 2017 21:04
@zpdDG4gta8XKpMCd
Copy link

thanks!

@@ -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)

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants