Dev Helpers
← Back

Separate display labels from IDs

Separate display labels from IDs using TypeScript.

Open this example in the interactive library

Source code

type Brand<T, Name extends string> = T & { readonly __brand: Name };
console.log(
  ((id: Brand<string, "UserId">, name: string) => ({ id, name }))(
    "u1" as Brand<string, "UserId">,
    "Ada",
  ),
);

Sample output

{ id: 'u1', name: 'Ada' }