Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

TypeScript branded types

1 view
Skip to first unread message

Mike Austin

unread,
Feb 13, 2025, 3:24:51 PMFeb 13
to PiLuD
type uuid = string & { __type: "uuid"; };

function uuid(value: string): uuid {
  if (!(...).test(value)) {
    throw Error("Not a valid UUID");
  }

  return value as uuid;
}

Now strings are not compatible with uuids. I had a feeling TypeScript could do it.

Reply all
Reply to author
Forward
0 new messages