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

number & { __brand } is not working with the comparison operator in nightly build #11643

Closed
zpdDG4gta8XKpMCd opened this issue Oct 14, 2016 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@zpdDG4gta8XKpMCd
Copy link

declare var x: number & { __brand: any };
x > 0 // <-- [ts] Operator '>' cannot be applied to types 'number & { __brand: any; }' and '0'.
@aozgaa aozgaa self-assigned this Oct 17, 2016
@aozgaa
Copy link
Contributor

aozgaa commented Oct 17, 2016

The error first appeared in a63a0d8.

@aozgaa
Copy link
Contributor

aozgaa commented Oct 19, 2016

Hey Aleksey,

This ties into an ongoing discussion about what we think about the use/addition of nominal types in our type system. There is a long list of issues related to nominal types.

It looks like this feature is actually working as designed, though the behavior is admittedly a bit counter-intuitive at first glance. Based on the recent addition of the comparability relation (issue #5517), specifically here, x isn't comparable to '0' because '0' is a numeric literal, and thus a subtype of 'number'. Here are two workarounds (inspired by your comment here :) ):

type BrandedNum = number & { __numberBrand: any };

declare var x : BrandedNum;
x > 0; // error: 0 is of numeric literal '0', a subtype of number
x > <number>0; // okay
x > <BrandedNum>0; // okay, in my opinion the clearest

In fact, this should be a general solution for dealing with "nominal typing" via brands: upcasting the other argument to a comparison operation to that of the two types' common ancestor will make the comparison pass our type check.

@aozgaa aozgaa added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Design Limitation Constraints of the existing architecture prevent this from being fixed labels Oct 19, 2016
@aozgaa aozgaa closed this as completed Oct 19, 2016
@aozgaa aozgaa reopened this Oct 19, 2016
@zpdDG4gta8XKpMCd
Copy link
Author

interesting, i will consider that

@aozgaa aozgaa removed the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Oct 19, 2016
@aozgaa aozgaa closed this as completed Oct 25, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants