uuidjs/uuid

Add UUID type to use in TypeScript

rodchenkov opened this issue · 1 comments

Feature description

Hi and thanks for your package.
I think it will be nice to have uuid type declaration or a class to be able to use it in TypeScript

import { UUID } from 'uuid';

export interface IEnvironment {
  production: boolean;
  currencyExchangeRate: ICurrencyExchangeRate;
}

export interface ICurrencyExchangeRate {
  baseAddress: string;
  currencyExchangePointId: UUID;
}

something like described here: https://stackoverflow.com/a/75962201/3730048

// use a brand to create a tagged type. Horrible hack but best we can do
export type UUID = string & { __uuid: void };

// uuid regex
const UUID_REGEX = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/;

// type guard to assert a string is a valid uuid
export function isUUID(uuid: string): uuid is UUID {
  return UUID_REGEX.test(uuid);
}

Additional information

No response

  1. This project doesn't (yet) have built-in support for TS types. Maybe some day, though.
  2. When it comes to dedicated "UUID" types, different projects are going to have differing needs. Deciding what the properties of such a type should / should not be and how it's used is a can of worms I have zero interest in dealing with.