guid/uuid v aplikácii Typescript Node.js


Snažím sa rozbehnúť balík uuid (v 3.0.1) v aplikácii Node/Typescript, ale nie som si istý, čo by som mal importovať a ako ho používať.

Toto je index.d.ts (z @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

Nemôžem tu nájsť exportovanú triedu.

Napríklad index.d.ts z angular2-uuid vyzerá takto:

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

A je to celkom zrejmé na použitie:

let id = UUID.UUID();

Tak. Ako používať (importovať a volať) uuid?

Odpoveď č. 1

Áno, tu je kód z môjho projektu:

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

Poznámka: na inštaláciu definícií je potrebné spustiť

npm install --save-dev @types/uuid

Odpoveď č. 2

Podľa testov:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/uuid/uuid-tests.ts

import uuid = require('uuid');

let uuidv4: string = uuid.v4();

Odpoveď č. 3

Použil som uuid pre svoj projekt v nasledujúcom poradí

Musí byť nainštalované

npm i --save-dev @types/uuid
npm install uuid

Používanie balíka uuid

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

Odpoveď č. 4

import * as uuid from "uuid";
const id: string = uuid.v4();

Súvisiace články