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

Shorter Implementation of Nominal #1

Open
MananTank opened this issue Jul 23, 2022 · 3 comments
Open

Shorter Implementation of Nominal #1

MananTank opened this issue Jul 23, 2022 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@MananTank
Copy link

const M = Symbol();

export type Nominal<Name extends string, Type> = Type & {
  readonly [M]: [Name];
};

Works with all the examples mentioned in readme.md

check it out :

https://codesandbox.io/s/nominal-types-l8k9ti?file=/src/nominal.ts

@CryogenicPlanet
Copy link
Member

Copying over the response from reddit https://www.reddit.com/r/typescript/comments/w5rebv/comment/ihbgoy3/?utm_source=share&utm_medium=web2x&context=3

We actually have a bug here, will try to patch later tonight

The reason we did the class thing was to try to get this to work

Mins = Nominal<"Mins", number>
Minutes = Nominal<"Mins", number>

const funcMins = (x:Mins) => {}

funcMins(10 as Mins) // works
funcMins(10 as Minutes) // should not work

To be clear, this is not actually working in our code rn. We screwed it up somewhere along the way

But the idea of using the function is NewSymbol is a different Symbol on each function "call" so typescript should ideally see them as different unique symbols. Two unique symbols with the same name will not match. The class is just a mechanism to pass a generic to a function, not doing much else

A basic example of the desired behaviours is here

Actually not super sure how to make an ergonomic way to achieve this off the top of my head, happy to brainstorm

@CryogenicPlanet CryogenicPlanet added the help wanted Extra attention is needed label Jul 23, 2022
@scalarhq scalarhq deleted a comment from issue-comment-bot bot Jul 23, 2022
@mochaaP
Copy link

mochaaP commented Oct 30, 2022

We actually have a bug here, will try to patch later tonight

@CryogenicPlanet any progress 😉?

@CryogenicPlanet
Copy link
Member

@mochaaP Unfortunately not, don't have a great way in mind to do this without making the UX of the tool much worse. And haven't had the time to spend too much time thinking about it

CryogenicPlanet added a commit that referenced this issue Mar 22, 2023
- Breaking Change: Remove nominal and nominal.make
- Use simpler implementation from #1 credit to @MananTank as this solves the "error TS2527: The inferred type of 'nominal' references an inaccessible 'unique symbol' type. A type annotation is necessary."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants