skip to content

🪪 Using TypeScript's In-Built Uuid Generator

TypeScript's randomUUID function and Uuid type make it easy to generate and work with UUIDs in code, improving readability and reducing the risk of bugs.

Introduction

In this post, we’ll be exploring how randomUUID function and the Uuid type can be used to generate and work with UUIDs (Universally Unique Identifiers) in a TypeScript project. UUIDs are often used to uniquely identify resources, such as database records or network sockets, and the randomUUID function and Uuid type make it easy to generate and work with them in TypeScript.

Code/TLDR

export function NewUuid() {
  return crypto.randomUUID() as Uuid;
}
 
export type Uuid = string & {
  [TYPE: symbol]: 'Uuid';
};
export function NewUuid() {
  return crypto.randomUUID() as Uuid;
}
 
export type Uuid = string & {
  [TYPE: symbol]: 'Uuid';
};

The NewUuid function uses the crypto.randomUUID() function to generate a random UUID and returns it as a value of type Uuid. The Uuid type is a custom string type that is defined with a symbol property to indicate that it is a UUID. This allows you to type-check UUIDs in your code to ensure that they are being used correctly.

Type-Safety

The purpose of the Uuid type is to provide a type annotation for UUIDs in the code, which can make the code easier to read and understand. It can also help catch potential bugs by providing type checking. For example, if a developer tries to assign a value that is not a UUID to a variable with the type Uuid, the TypeScript compiler will throw an error.

// A function that expects a Uuid as an argument:
function deleteUser(userId: Uuid) {
  // 🗑️ Delete User Code
}
 
// 🙅 Incorrect usage:
// This will cause a TypeScript error, because '123' is not a Uuid
deleteUser('123');
 
// 👍 Correct usage:
// This is valid, because userId has the type Uuid
const userId = NewUuid();
deleteUser(userId);
// A function that expects a Uuid as an argument:
function deleteUser(userId: Uuid) {
  // 🗑️ Delete User Code
}
 
// 🙅 Incorrect usage:
// This will cause a TypeScript error, because '123' is not a Uuid
deleteUser('123');
 
// 👍 Correct usage:
// This is valid, because userId has the type Uuid
const userId = NewUuid();
deleteUser(userId);

In this example, the deleteUser() function expects a Uuid as its argument. When the userId variable, which has the type Uuid, is passed as an argument, the code is valid and no errors are thrown. However, if a regular string value is passed as an argument, the TypeScript compiler will throw an error, because the regular string value is not considered a valid Uuid.

Conclusion

In conclusion, the randomUUID function and the Uuid type are powerful tools for generating and working with UUIDs in TypeScript. By using the randomUUID function, you can easily generate new UUIDs in your code, and by using the Uuid type, you can improve the readability and maintainability of your code. Whether you are working with databases, network sockets, or any other resources that require unique identifiers, the randomUUID function and Uuid type can help you get the job done.