Skip to content

Consistently reduce union types#2729

Merged
ahejlsberg merged 9 commits into
masterfrom
reducedUnionTypes
Apr 14, 2015
Merged

Consistently reduce union types#2729
ahejlsberg merged 9 commits into
masterfrom
reducedUnionTypes

Conversation

@ahejlsberg
Copy link
Copy Markdown
Member

Union types are now consistently reduced to the smallest possible set before members (properties or signatures) are accessed.

Fixes #2610.

Conflicts:
	src/compiler/types.ts
	tests/baselines/reference/APISample_compile.js
	tests/baselines/reference/APISample_compile.types
	tests/baselines/reference/APISample_linter.js
	tests/baselines/reference/APISample_linter.types
	tests/baselines/reference/APISample_transform.js
	tests/baselines/reference/APISample_transform.types
	tests/baselines/reference/APISample_watcher.js
	tests/baselines/reference/APISample_watcher.types
Comment thread src/compiler/checker.ts
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.

Split each branch onto its own line:

return type.flags & TypeFlags.Union ?
    getPropertiesOfUnionType(<UnionType>type) :
    getPropertiesOfObjectType(type);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or, preferably:

return type.flags & TypeFlags.Union
    ? getPropertiesOfUnionType(<UnionType>type)
    : getPropertiesOfObjectType(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.

👍 to @CyrusNajmabadi's suggestion

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.

Fits just fine on one line, I'd prefer to keep it that way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The problem with long single lines is github:

image

It's actually harder to read because github is optimized for 80 columns.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

Does this fix: #1953 as well?

If not, is there something we can do while we're addressing consistent reduction to also address the nondeterminism issue as well?

@ahejlsberg
Copy link
Copy Markdown
Member Author

No, this isn't related to #1953. We need a different solution for that.

Comment thread src/compiler/checker.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it at all significant that this case moved to the end of the function? To my reading, it is not.

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.

No, it is not significant, but I like handling the union case after the object case (as it is less common).

@JsonFreeman
Copy link
Copy Markdown
Contributor

Do we also need to force subtype reduction in applyToContextualType?

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Apr 14, 2015

👍

@ahejlsberg
Copy link
Copy Markdown
Member Author

@JsonFreeman Regarding subtype reduction in applyToContextualType... No, I don't think we want to do that. When a contextual type is a union type, keeping the all constituents of the union gives us more information. For example, say you have a contextual type A | B, where B is a subtype of A, for some object literal. If B declares some property foo, then keeping B around allows us to contextually type a foo property in the object literal.

@JsonFreeman
Copy link
Copy Markdown
Contributor

Your argument about applyToContextualType makes sense. However, we cannot take advantage of that capability consistently, because the fact is that normally we do collapse the union types when we create them. So in many cases, by the time we get to applyToContextualType, it is already too late and we've lost some of the properties.

@ahejlsberg
Copy link
Copy Markdown
Member Author

@JsonFreeman I think we just need to modify the spec to document exactly when subtype reduction occurs. Or, conversely, when it doesn't occur. The latter may be easier as the only time we defer subtype reduction is in an explicitly written type.

@JsonFreeman
Copy link
Copy Markdown
Contributor

And instantiation, but I guess that is a process that's not explained in the spec.

Yes, I think it makes sense to spec subtype reduction, just like how we spec "depends on" for type aliases.

@JsonFreeman
Copy link
Copy Markdown
Contributor

👍

ahejlsberg added a commit that referenced this pull request Apr 14, 2015
@ahejlsberg ahejlsberg merged commit 6d36dd5 into master Apr 14, 2015
@ahejlsberg ahejlsberg deleted the reducedUnionTypes branch April 14, 2015 22:24
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.

Union types are NOT reduced to the smallest possible set

6 participants