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

Another patch for Nominal Type #570

Open
andresmoschini opened this issue Mar 12, 2020 · 3 comments
Open

Another patch for Nominal Type #570

andresmoschini opened this issue Mar 12, 2020 · 3 comments

Comments

@andresmoschini
Copy link

In this article, as a introduction to another thing, the author shows another kind of patch to use nominal types:

interface PersonIdBrand { _type: "Person"; }
type PersonId = PersonIdBrand & number;

interface BlogPostIdBrand { _type: "BlogPost"; }
type BlogPostId = number & BlogPostIdBrand;

It worked better for me with type inference than your approach with enums.

Do you think that is a good idea to add this alternative? or is there a well known issue with this approach?

@andresmoschini
Copy link
Author

andresmoschini commented Mar 12, 2020

The type inference works better for me in this scenario:

type A = string & { _type: 'A' };
type B = string & { _type: 'B' };
type C = string & { _type: 'C' };
type D = A | B | C;

const f = (a: A, b: B, c: C) => a || b || c; 
// Infers: const f: (a: A, b: B, c: C) => D ✅
enum ABrand {  _ = '' }
type A = string & ABrand;
enum BBrand {  _ = '' }
type B = string & BBrand;
enum CBrand {  _ = '' }
type C = string & CBrand;
type D = A | B | C;

const f = (a: A, b: B, c: C) => a || b || c; 
// Infers: const f: (a: A, b: B, c: C) => C 💔

@andresmoschini
Copy link
Author

andresmoschini commented Mar 12, 2020

An alternative, more similar to your Interface related one is:

type A = string & { _aBrand: any; }
type B = string & { _bBrand: any; }
type C = string & { _cBrand: any; }
type D = A | B | C;

const f = (a: A, b: B, c: C) => a || b || c;
// Infers: const f: (a: A, b: B, c: C) => D ✅

@basarat
Copy link
Owner

basarat commented Mar 18, 2020

👍 Needs to be added as Using intersection types at some point 🌹

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

No branches or pull requests

2 participants