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

How to make more better structural branded types #8510

Closed
falsandtru opened this issue May 7, 2016 · 3 comments
Closed

How to make more better structural branded types #8510

falsandtru opened this issue May 7, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@falsandtru
Copy link
Contributor

falsandtru commented May 7, 2016

It can create lots of new types easily and structurally.

function build<N, T>() {
  return <{
    (a: N & T): void;
    (a: T): N & T;
  }>((a: T) => a);
}

namespace Types {
  export namespace Nominal1 {
    class Nominal<T> {
      private NOMINAL: T;
    }
    export type A = Nominal<'A'> & number;
    export const A = build<Nominal<'A'>, number>();
    export type B = Nominal<'B'> & number;
    export const B = build<Nominal<'B'>, number>();
  }
}

namespace Types {
  export namespace Nominal2 {
    class Nominal<T> {
      private NOMINAL: T;
    }
    export type A = Nominal<'A'> & number;
    export const A = build<Nominal<'A'>, number>();
    export type B = Nominal<'B'> & number;
    export const B = build<Nominal<'B'>, number>();
  }
}

var a1: Types.Nominal1.A;
var b1: Types.Nominal1.B;
var a2: Types.Nominal2.A;
a1 = b1; // error
a1 = a2; // error
a1 = Types.Nominal1.A(0); // ok
a1 = Types.Nominal1.A(a1); // error
a1 = Types.Nominal1.B(0); // error
a1 = Types.Nominal2.A(0); // error
@mhegazy
Copy link
Contributor

mhegazy commented May 7, 2016

looks like the same topic as in #4895. i suggest moving the discussion there.

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 7, 2016
@falsandtru
Copy link
Contributor Author

This issue posted only for documentation. This is a different technique, and has a different purpose from #4895.

@mhegazy
Copy link
Contributor

mhegazy commented May 20, 2016

This issue posted only for documentation. This is a different technique, and has a different purpose from #4895.

I understand; #4895 is a discussion item that is meant for brainstorming and sharing ideas.

@mhegazy mhegazy closed this as completed May 20, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants