News for hackers. Stuff that matters.
Re:Opaque Types in Flow and TypeScript by wakeywakeywakey on Saturday December 16, 2017 @06:35AM (#15939058)
In TypeScript, you can also use an empty const enum to simulate nominal typing for opaque types. It gets inlined/removed during compilation:

  const enum _A = {};
  type A = number & _A;
This comes in handy for IDs and other primitives shared by multiple complex types.
Re:Opaque Types in Flow and TypeScript by koblas on Saturday December 16, 2017 @09:31AM (#15939675)
I would love to see an extended example. When I tried this in typescript 2.6 it still made PaymentAmount and AccountNumber equivalent types.
Re:Opaque Types in Flow and TypeScript by justhelpingout on Saturday December 16, 2017 @10:20AM (#15939888)
You can certainly do this - you just need something to contain the enum since an enum is a nominal subtype of `number` already, so TypeScript unifies `number & anyEnum` to `number`.

The best tagging mechanism these days is `Nominal<T>` (which has no emit) or `As<T>` (which has a minimal amount of emit):

    interface Nominal<T> {
      'nominal type tag': T
    }

    class As<T> {
      private tag: T
    }
Both are used in the same manner:

    const enum SomeTag { }
    type Something = number & As<SomeTag>
    // or
    type Something = number & Nominal<SomeTag>
You can even avoid the boilerplate of `const enum` if you make the `T` of `As` or `Nominal` `T extends string`:

    type Something = number & As<'some:unique:string:tag'>
In both cases, the values are not unified, so:

    type Something = number & As<'kind1'>
    type SomethingElse = number & As<'kind2'>
    var x: Something = 123 as Something;
    var y: SomethingElse = 456 as SomethingElse;
    x = y;  // Type 'SomethingElse' is not assignable to type 'Something'.
Re:Opaque Types in Flow and TypeScript by TomMarius on Saturday December 16, 2017 @02:17PM (#15941300)
BTW the minimal emit of the class-based example will be removed by Webpack since the runtime representation is never used. Best option if you use a bundler.
Re:Opaque Types in Flow and TypeScript by saurik on Saturday December 16, 2017 @10:07AM (#15939833)
I have not tried it yet, but this technique is documented as the second pattern on this website:

https://basarat.gitbooks.io/typescript/docs/tips/nominalTypi...

(edit:) Ah ha! "Because numbers are type compatible with enums the previous technique cannot be used for them."