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

[iov-types] Enum tagging of basic types #9

Merged
merged 10 commits into from
May 2, 2018
Merged

Conversation

will-iov
Copy link
Contributor

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 to PublicKeyString).

Disadvantages

  • Using an empty enum is pretty counterintuitive, not very semantic. (Maybe another solution comes along via Tag types microsoft/TypeScript#4895 .)
  • Compared to aliases (e.g. to a normal string), it requires lots of casting.

Advantages

  • Besides the enum issue, actually very semantic: a PrivateKeyString is a PrivateKey and a string: we even get to see the relationship between e.g. private key buffers and strings.
  • Doesn't pollute the source code outside of the typing system.

@will-iov will-iov requested a review from ethanfrey April 27, 2018 19:32
Copy link
Contributor

@ethanfrey ethanfrey left a 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.

@will-iov
Copy link
Contributor Author

will-iov commented May 2, 2018

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 unique symbol. I'll merge this for now and make a new PR switching to unique symbol for further discussion.

@will-iov will-iov merged commit 027053b into master May 2, 2018
@ethanfrey
Copy link
Contributor

ethanfrey commented May 2, 2018

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

@will-iov will-iov deleted the enum-private-public-keys branch May 2, 2018 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants