Skip to content
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

ts2.1: handle boolean types correctly #314

Merged
merged 1 commit into from
Jan 10, 2017
Merged

ts2.1: handle boolean types correctly #314

merged 1 commit into from
Jan 10, 2017

Conversation

evmar
Copy link
Contributor

@evmar evmar commented Jan 10, 2017

Booleans are represented as a union of true|false, but they
also have the boolean flag set so our existing code that handled
unions wasn't firing.

If we just treat them as unions, everything works, including more
complex cases (like boolean|number => true|false|number, without
the boolean flag set).

More work on #295. With this change, the remaining failures in
tests are mostly whitespace.

Booleans are represented as a union of true|false, but they
also have the boolean flag set so our existing code that handled
unions wasn't firing.

If we just treat them as unions, everything works, including more
complex cases (like boolean|number => true|false|number, without
the boolean flag set).

More work on #295.  With this change, the remaining failures in
tests are mostly whitespace.
// Note also that in a more complex union, e.g. boolean|number, then
// it's a union of three things (true|false|number) and
// ts.TypeFlags.Boolean doesn't show up at all.
if (type.flags & ts.TypeFlags.Union) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am confused here. You say that for booleans Union and Boolean flags are on. Since more than one flag is one we are in the 'default' case. Then we are in this case too, and we output 'true | false', but don't we want 'boolean' for booleans?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The union is true|false in TS, which becomes boolean|boolean in Closure, which we dedup to boolean.

The thing I was trying to point out in this comment is that by handling the union it works everywhere, so we don't need to special-case boolean.
E.g. if the input is boolean|number, then it becomes true|false|number, which we convert to Closure boolean|boolean|number and then dedup to boolean|number.

Do you have advice on how to make the comment better?

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense, I was expecting special casing boolean. My reasoning is that if closure ever adds 'true' and 'false', this code will emit 'true|false' for input 'boolean'.

But even with special case boolean, closure adding 'true' would still break on 'boolean | number' because it will emit 'true | false | number'. So I am not going to worry about it.

@evmar evmar merged commit 6367273 into ts2.1 Jan 10, 2017
@evmar evmar deleted the ts2.1-union branch January 10, 2017 22:54
rkirov pushed a commit that referenced this pull request Jan 13, 2017
Booleans are represented as a union of true|false, but they
also have the boolean flag set so our existing code that handled
unions wasn't firing.

If we just treat them as unions, everything works, including more
complex cases (like boolean|number => true|false|number, without
the boolean flag set).

More work on #295.  With this change, the remaining failures in
tests are mostly whitespace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants