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

Make the Opaque type stricter #71

Merged
merged 6 commits into from Jan 31, 2020
Merged

Make the Opaque type stricter #71

merged 6 commits into from Jan 31, 2020

Conversation

resynth1943
Copy link
Contributor

This commit adds a Token parameter to the Opaque types, allowing them to be
fundamentally unique. This means an oaque type cannot be cast to a separate
opaque type. The previous (flawed) design allowed this. This does not.

Closes #70.

Will Hoskings added 2 commits January 19, 2020 21:19
This commit adds a Token parameter to the Opaque types, allowing them to be
fundamentally unique. This means an oaque type cannot be cast to a separate
opaque type. The previous (flawed) design allowed this. This does not.
@resynth1943
Copy link
Contributor Author

Should I eslint-ignore that error? Also, I'm seeing linting errors for other tests. Not sure what's going on there.

Copy link
Contributor

@papb papb left a comment

Choose a reason for hiding this comment

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

I think you should also add an explicit test that they are really opaque, i.e., add a test that resembles the first post of the issue

source/opaque.d.ts Outdated Show resolved Hide resolved
source/opaque.d.ts Outdated Show resolved Hide resolved
@resynth1943
Copy link
Contributor Author

The thing is, I believe my test resembles the first post of the issue well. It shows opaque types are not interchangeable, which is the desired effect.

@sindresorhus
Copy link
Owner

Because TypeScript supports recursive types, the Token could be the parent type itself. This could create even stronger unique types. Would be nice with an example in the docs and a test for this. For example:

type Person = {
	id: Opaque<number, Person>;
	name: string;
};

@resynth1943
Copy link
Contributor Author

@sindresorhus Yes! 😛 I'll add something like that into the TSDoc block.

Thanks.

@resynth1943
Copy link
Contributor Author

Damn. We gotta get this merged.

@resynth1943
Copy link
Contributor Author

/bing @sindresorhus @papb

@resynth1943
Copy link
Contributor Author

Also, I'd like to give apologies for my absence. I've currently been doing my mock examinations in school. Anyway, that's over now, and I'm back. 😛

@sindresorhus sindresorhus changed the title Implement stronger opaque types Make the Opaque type stricter Jan 31, 2020
@sindresorhus sindresorhus merged commit 3ba4cc1 into sindresorhus:master Jan 31, 2020
@sindresorhus
Copy link
Owner

Thanks :)

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.

Opaque types are interchangeable
3 participants