Generate a UUID in TypeScript

TypeScript is a programming language that is a JavaScript superset. TypeScript code is transpiled to JavaScript so that it runs with standard JavaScript engines.

TypeScript was first developed by Microsoft and then released publicly in 2012 as an open-source project. The goal of the language was to make it easier to develop large, complex applications in JavaScript. TypeScript supports classes and static typing, among other programming language constructs not found in older JavaScript language standards, leading to better structured code and improved support in development tools such as IDEs.

How to Generate a UUID in TypeScript

Although neither the TypeScript nor JavaScript language have built-in support for generating a UUID or GUID, there are plenty of quality 3rd party, open-source libraries that you can use.

The JavaScript library we recommend for generating UUIDs in TypeScript is called (unsurprisingly), uuid. It can generate version 1, 3, 4 and 5 UUIDs.

Installing the uuid Package

To get started with the library, you'll need to install it. If you're working in a project with a package.json file, run this command to add it to the dependencies list:

% npm install uuid

Or if you want to install it globally on your computer, use this command:

% npm install -g uuid

Generating UUIDs

With the uuid library installed, you can now use it in your TypeScript code.

Here's how you can generate a version 4 UUID:

import {v4 as uuidv4} from 'uuid';
let myuuid = uuidv4();
console.out('Your UUID is: ' + myuuid);

Explanation

  • Line #1 imports the version 4 UUID function. There are also functions available for generating version 1, 3 and 5 UUIDs.
  • Line #3 generates the UUID and saves it in the variable, myuuid.
  • The output from line #5 will be something like:
    Your UUID is: 8a6e0804-2bd0-4672-b79d-d97027f9071a
    
  • NOTE: There are TypeScript typings for the uuid JavaScript library. However, as the library is small and typical usage does not deal with any custom types, adding the typings to your TypeScript project is not strictly needed.

    If you would like to install the TypeScript typings for the uuid Javascript library, you can use this command:

    % npm install -D @types/uuid

The uuid library has a number of other functions, such as for converting from a string representation of a UUID into a byte array and back. You can read more about the JavaScript uuid library on it's GitHub page.


How can we improve this page? Let us know!




The UUIDs generated by this site are provided AS IS without warranty of any kind, not even the warranty that the generated UUIDs are actually unique. You are responsible for using the UUIDs and assume any risk inherent to using them. You are not permitted to use the UUIDs generated by this site if you do not agree to these terms. Do not use any UUIDs found on cached versions of this page.
Privacy Policy
This website uses cookies. We use cookies to personalise content/ads and to analyse our traffic.

The UUIDs generated by this site conform to RFC 4122 whenever possible.
Read more about UUIDs at Wikipedia.
Check out our developer API.

Copyright © 2024 TransparenTech LLC. All Rights Reserved. Contact Us.