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
Opaque types are interchangeable #70
Comments
The example shown in https://codemix.com/opaque-types-in-javascript/ works as expected. Their version of type Opaque<K, T> = T & { __TYPE__: K }; With that definition they are not interchangeable. So I think this is a bug? |
// @resynth1943 |
That's not the same as the definition of the export type Opaque<Type> = Type & {readonly __opaque__: unique symbol}; I think I know why TypeScript is doing this...
Now, as you can see, After some minutes of exploration, I think I've found a solution: type Opaque<Type, Marker extends string> = Type & Record<Marker, 'opaque'>;
type A = Opaque<number, 'A'>;
type B = Opaque<number, 'B'>;
function useA(a: A) { }
function getA(): A {
return 2 as A;
}
const a: A = getA();
const b: B = getA(); // Compilation error.
useA(a); In short, providing unique names to the @sindresorhus How do you feel about this? EDIT: Oops. They're not the same. |
If everyone's happy with my proposal above, I'd be happy to make a Pull Request. Following this, perhaps making the second generic parameter default to |
I’m happy with that proposal, including defaulting the second parameter to |
Well if everyone's happy, I'm happy to blow the cobwebs off VSCodium and PR this in. ❤️ |
👍 |
Thank you! |
Do you have any examples for
Opaque
? I thoughtOpaque
types weren’t supposed to be interchangeable.Upvote & Fund
The text was updated successfully, but these errors were encountered: