menu
116 tokens

Newtypes

Proper nominality and newtypes.

Newtypes

  • usually use symbol branding in TS, which is kinda icky
1const BrandTypeId: unique symbol = Symbol.for("effect/Brand")2 3type ProductId = number & {4  readonly [BrandTypeId]: {5    readonly ProductId: "ProductId" // unique identifier for ProductId6  }7}
  • proper nominality and newtypes
  • newtype, newtype interfaces (traits)
1newtype UserId = string;2const userId = UserId("123");
1newtype UserId = private string; // can only be constructed within file