-
Notifications
You must be signed in to change notification settings - Fork 18
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
[iov-types] Enum tagging of basic types #9
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks quite nice and you achieve plenty with the const enum
and as
.
My comment on github got a link to this response: microsoft/TypeScript#202 (comment) and it seems even more "idiomatic" to replace eg.
const enum PublicKey {}
with
declare const PublicKey: unique symbol
not sure the difference, but seems like some smart ts people were discussing this.
Ooh, that could be a better approach, definitely more idiomatic. It would be nice if it was documented somewhere! The best I found on the typescript website was a changelog with a pretty minimal mention of |
I added that new pr already :) But yeah, let's discuss approaches there, this was ready to merge and that change doesn't affect the public api |
Proposal for handling subtypes using
const enum
for tagging. Note that specifying the type of a variable in addition to casting is probably overkill in most situations, just leaving it in here so you can see the compatibility (or incompatibility if you change the type of the variable from e.g.PrivateKeyString
toPublicKeyString
).Disadvantages
enum
is pretty counterintuitive, not very semantic. (Maybe another solution comes along via Tag types microsoft/TypeScript#4895 .)string
), it requires lots of casting.Advantages
enum
issue, actually very semantic: aPrivateKeyString
is aPrivateKey
and astring
: we even get to see the relationship between e.g. private key buffers and strings.