Skip to content

Remove empty object types from intersection types#15033

Merged
ahejlsberg merged 3 commits into
masterfrom
emptyObjectIntersection
Apr 5, 2017
Merged

Remove empty object types from intersection types#15033
ahejlsberg merged 3 commits into
masterfrom
emptyObjectIntersection

Conversation

@ahejlsberg

Copy link
Copy Markdown
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

@sandersn sandersn left a comment

Copy link
Copy Markdown
Member

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 }

Comment thread src/compiler/checker.ts
!t.numberIndexInfo;
}

function isEmptyObjectType(type: Type) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

thanks!

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

Copy link
Copy Markdown

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.

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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