Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

guid/uuid in Typescript Node.js app

I try to make a uuid (v 3.0.1) package work in Node/Typescript app, but I'm not sure what should I import and how to use it.

This is index.d.ts (from @types/uuid v 2.0.29):

declare namespace uuid {     interface V1Options {         node?: number[];         clockseq?: number;         msecs?: number | Date;         nsecs?: number;     }      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 

I cant find exported class here.

For example index.d.ts from angular2-uuid looks like that:

export declare class UUID {     constructor();     static UUID(): string;     private static pad4(num);     private static random4(); } 

And it's quite obvious to use:

let id = UUID.UUID(); 

So. How to use (import and call) uuid?

like image 524
tBlabs Avatar asked May 07 '17 23:05

tBlabs


People also ask

Does TypeScript have GUID?

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.

What is UUID in node JS?

UUIDs are typically used as unique identifiers. You can also use them in JavaScript and Node. js. The Node. js team recently added native support to generate a UUID to Node.

Why we use UUID in node JS?

In Node. js there are many ways to generate a UUID. One of them is with a native module and others are using NPM packages. UUID can be very useful as reliable unique identifiers.

What is the use of UUID NPM?

NPM(Node Package Manager) is a package manager of Node. js packages. There is an NPM package called 'shortid' used to create short non-sequential url-friendly unique ids.


1 Answers

Yes, here is code from my project:

import { v4 as uuid } from 'uuid'; const id: string = uuid(); 

Note: to install definitions it is required to run

npm install --save-dev @types/uuid 
like image 137
Sergey Yarotskiy Avatar answered Sep 19 '22 13:09

Sergey Yarotskiy