Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UUID type to use in TypeScript #741

Closed
rodchenkov opened this issue Jan 5, 2024 · 1 comment
Closed

Add UUID type to use in TypeScript #741

rodchenkov opened this issue Jan 5, 2024 · 1 comment

Comments

@rodchenkov
Copy link

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

@broofa
Copy link
Member

broofa commented Jan 5, 2024

  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.

@broofa broofa closed this as completed Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants