What is @types/uuid?
The @types/uuid package provides TypeScript type definitions for the uuid package, enabling developers to generate and work with UUIDs (Universally Unique Identifiers) in a type-safe manner. It supports various versions of UUIDs and offers functionalities like generating and validating UUIDs.
What are @types/uuid's main functionalities?
Generating UUID v4
This feature allows for the generation of random UUIDs (version 4). The code sample demonstrates how to import the v4 function from the uuid package and use it to generate a new UUID.
import { v4 as uuidv4 } from 'uuid';
const myUuid = uuidv4();
Validating UUIDs
This feature enables the validation of UUID strings to check if they are valid UUIDs. The code sample shows how to import the validate function from the uuid package and use it to validate a given UUID string.
import { validate as uuidValidate } from 'uuid';
const isValid = uuidValidate('a3bb189e-8bf9-3888-9912-ace4e6543002');
Generating UUID v1
This feature supports the generation of time-based UUIDs (version 1). The code sample illustrates how to import the v1 function from the uuid package and use it to generate a new UUID.
import { v1 as uuidv1 } from 'uuid';
const myUuid = uuidv1();
Other packages similar to @types/uuid
shortid
Shortid is a package that generates short, non-sequential, URL-friendly unique ids. Unlike @types/uuid, which focuses on providing type definitions for UUIDs, shortid is designed for shorter ids and does not provide type definitions out of the box.
nanoid
Nanoid is a tiny, secure, URL-friendly, unique string ID generator for JavaScript. It offers a similar functionality to uuid in generating unique identifiers but focuses on being smaller and faster. Nanoid also does not require type definitions to be installed separately, as it includes TypeScript support natively.
Installation
npm install --save @types/uuid
Summary
This package contains type definitions for uuid (https://github.com/defunctzombie/node-uuid).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/uuid/v2.
declare namespace uuid {
interface V1Options {
node?: number[] | undefined;
clockseq?: number | undefined;
msecs?: number | Date | undefined;
nsecs?: number | undefined;
}
type V4Options = { random: number[] } | { rng: () => number[] };
interface UuidStatic {
(options?: V4Options): string;
(options: V4Options | null, buffer: number[], offset?: number): number[];
(options: V4Options | null, buffer: Buffer, offset?: number): Buffer;
v1(options?: V1Options): string;
v1(options: V1Options | null, buffer: number[], offset?: number): number[];
v1(options: V1Options | null, buffer: Buffer, offset?: number): Buffer;
v4: UuidStatic;
parse(id: string): number[];
parse(id: string, buffer: number[], offset?: number): number[];
parse(id: string, buffer: Buffer, offset?: number): Buffer;
unparse(buffer: number[] | Buffer, offset?: number): string;
}
}
declare const uuid: uuid.UuidStatic;
export = uuid;
Additional Details
- Last updated: Tue, 07 Nov 2023 20:08:00 GMT
- Dependencies: @types/node
Credits
These definitions were written by Oliver Hoffmann.