-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Add Transparent to reverse Opaque #165
Comments
Yeah, this sounds like a useful type to me. Note that generalized index types are planned for TS 4.2, which might (did not test, just looked relevant) solve your specific problem: microsoft/TypeScript#26797 |
// @voxpelli @kainiedziela Thoughts? |
Makes sense to me 👍 Seems like a good addition, to be able to go both ways. Is the name “Transparent” the best or would “DeOpaque”, “UnOpaque” or similar maybe show a clearer connection between the two and make it easier to find and understand + “pollute” the overall exports list a bit less? Even though such a wording might be linguistically less than ideal |
I was thinking about |
could do excited to see this land! and didn't know about the generalized type update in 4.2, looking forward to that! |
Some other alternatives:
|
I'm a fan of |
If it ends up standing between |
IMO the best naming convention would be to deprecate
|
I got it! Unpaque™ As in unpacking an Opaque value. |
On a related note: https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing |
Then I suggest we go with |
And maybe keep Opaque as an alias for a version or two? Adding |
Yup |
Actually, maybe we could make the I found this in some random TS playground: declare const USERID_SYMBOL: unique symbol;
declare const ITEMID_SYMBOL: unique symbol;
declare const TAG_SYMBOL: unique symbol;
type Opaque<T, U extends symbol> = T & { readonly [TAG_SYMBOL]: U };
type UserId = Opaque<number, typeof USERID_SYMBOL>;
type ItemId = Opaque<number, typeof ITEMID_SYMBOL>; Any thoughts on this approach? Since it uses a unique symbol, it should be completely opaque as long as the symbol is not exposed. The |
Is there any difference between nominal and opaque types? microsoft/TypeScript#202 |
Here's a discussion around all that: https://stackoverflow.com/a/56749647 Throw in a |
Through microsoft/TypeScript#202, I also discovered another apporach for declare class Tagged<Token> {
protected readonly __opaque__: Token;
}
export type Brand<Type, Token = unknown> = Type & Tagged<Token>; The benefit here over our current approach is that the Not sure if we could make |
Sure |
Opaque nominal types are incredibly useful, but there are times where you need to un-opaque a type ignorer to use it (e.g. as a key in an object).
I propose the addition of
fails now:
but this would fix that error without completely making Opaque types useless or overly compromising their contract:
The text was updated successfully, but these errors were encountered: