Comments (22)
Exciting => microsoft/TypeScript#33038
from type-fest.
Possible names:
- Opaque
- Newtype
- Brand
I prefer Newtype
as itβs the most descriptive one.
from type-fest.
I don't feel strongly about it and I'm ok with Opaque
too, so we can go for that. PR welcome :)
from type-fest.
@resynth1943 It would be the same for flow actually
To my understanding, the reason is that functions, by nature, are
- covariant for arguments (that's why it's not enough to pass a number as an argument, because it doesn't have
__opq__
prop), unless specified otherwise (e.g. with exact types it will be invariant) - contravariant by return type, in a sense that we can just cut the strange part with
__opq__
and leave a number, cause it's a part of intersection
from type-fest.
I don't think it's worth doing a breaking release now, since TS will get support for this anyway, which would eventually make our type moot.
from type-fest.
I like it.
This sounds like newtype
in Haskell: https://stackoverflow.com/questions/17591276/what-programming-languages-have-something-like-haskell-s-newtype
Relevant reading:
- microsoft/TypeScript#15807
- https://codemix.com/opaque-types-in-javascript/
- https://michalzalecki.com/nominal-typing-in-typescript/
Another version:
type Brand<K, T> = K & { __brand: T }
type USD = Brand<number, "USD">
type EUR = Brand<number, "EUR">
from type-fest.
I would be happy to send a PR. All good?
from type-fest.
I'd actually prefer Opaque
, it decribes best what this type does for people who don't know Haskell. I find Newtype
makes sense for people with Haskell background but what should it mean for someone who doesn't know Haskell? A new type? This could be anything.
from type-fest.
My problem with Opaque
is that it has a different meaning in Swift: https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html
For me, Newtype
reads as creating a new type based on the given type.
from type-fest.
Yeah, I'm happy to use that name, since it follows the same concept as my original proposal.
from type-fest.
@BendingBender Yeah, that's a good point. Flow uses opaque
to describe an opaque type, so Opaque
might be more familiar to people using Flow, or TypeScript.
I do think that people could just check the README to see what the type means, if they're unsure.
from type-fest.
Oh, didn't know about Swift. As far as I understand it, it's not the same in Swift but also not that different. In Swift it hides the specific type returned from a function. This goes a bit in the same direction.
In Haskell, a newtype
is also not necessarily an opaque type, it's just a type that must have exactly one type parameter and one constructor. Opaque types in Haskell are types whose constructors are not exposed from a module, this can be newtype
s but also plain data
types can be opaque.
from type-fest.
For me, Newtype reads as creating a new type based on the given type.
You're right. But what I'm trying to say is that it doesn't tell the user what's the purpose of this new type. Opaque
is a tad clearer on this.
from type-fest.
from type-fest.
@sindresorhus I've sent the Pull Request. #53
from type-fest.
@resynth1943 Sorry for bothering, but I think Opaque is unfortunate name for it. The reason is that Opaque by definition hides the internal data structure (that's why it's opaque) so you can't do anything useful with it without the functions that can extract the underlying data from the opaque type. Much like in languages with classes and private fields you can't access or interact with them unless a class will provide a method for it. Current implementation totally allows to get an access to the underlying structure and even focuses on making it easy (via intersection). I'd say Brand is pretty good name cause I've it used for such in various places of TS community. I'm researching the theory and other terms for it.
from type-fest.
@saitonakamura What do you think about the name Unique
? Seeing as TS intends to use the unique
keyword.
from type-fest.
@saitonakamura I'd be interested to hear what you think is a better name for this. I obtained the name "opaque" from Flow, which employs the use of the same name.
from type-fest.
@resynth1943 sorry if it's already clear for you, but for the sake of clarity: the flow opaque
type is truly opaque. The same example with the current definition would result in no error (it's kinda hard to implement modules in a playground, but if you wanna play, here is ts playground link)
type Opaque<V> = V & { readonly __opq__: unique symbol };
type NumberAlias = Opaque<number>
function convert(x: NumberAlias): number {
return x; // No error
}
@sindresorhus , @resynth1943 Unique
does sound like a good name to me, exploiting the ts own naming. Brand
is commonly (well, as much commonly as other advanced type aspects in TS world) used across articles I've seen in the wild. Another option would be Nominal
, but it could be unclear to the person who's not familiar with the structural/nominal typing topic.
Honestly, I don't have any preference over those three, I mean it's just naming, I think as long as it's not confusing it's fine. But if you'll point a gun at my foot and ask to choose one I'd go for Unique
.
Let me know what option you're ok with, i can submit a PR if you want
from type-fest.
I'm not sure I know enough about this, but I'm confused as to why Typescript ignores the __opq__
marker in the code you posted above.
from type-fest.
Ok, thank you for the discussion π
from type-fest.
I have to say, though, all this is confusing. You would think TypeScript would treat everything the same
from type-fest.
Related Issues (20)
- Add `SetReadonly` HOT 3
- NonEmptyObject HOT 1
- The peer deps of `typescript` causes it installed in production mode HOT 8
- Helper for `fn(...rest, extra)` ("A rest parameter must be last in a parameter list.") HOT 10
- 'Type instantiation is excessively deep..' error caused by update to `SetRequired<>` in v3.10.0
- Jsonify: `readonly` messes with Jsonified tuple types HOT 2
- KeysOfType HOT 1
- Is there a way to check that a key conforms to a base type without using an interface? HOT 2
- `FlattenedRecordDeep` type
- `Indices` type
- `LastArrayElement<[T, ...U[]]>` should be `T | U`, but it is `U`. HOT 1
- `LastArrayElement<[...T[], U]>` should be `U`, but it is `T | U` HOT 1
- `Integer<1e+100>` should be `1e+100`, but it is `never`
- `Immutable` type
- Utility Type Request: DeepUndefinedToNull HOT 1
- `Opaque` does not work with case changes (ie. SnakeCasedProperties, etc) HOT 1
- Fails compilation in swc's strict mode
- Proposal: `RecordOf`
- `Entries` don't works like `ObjectEntries` HOT 1
- PartialDeep should skip HTMLCanvasElement HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from type-fest.