Typescript Node.js应用程序中的guid / uuid

时间:2017-05-07 23:08:15

标签: node.js typescript uuid guid

我尝试在Node / Typescript应用程序中使uuid(v 3.0.1)包工作,但我不确定应该导入什么以及如何使用它。

这是index.d.ts(来自@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

我在这里找不到导出的课程。

例如index.d.ts中的angular2-uuid看起来像这样:

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

使用起来非常明显:

let id = UUID.UUID();

因此。如何使用(导入和调用)uuid

3 个答案:

答案 0 :(得分:56)

是的,这是我项目的代码:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这也对我有用:

1[ContactsApp.Contact]', but this
  dictionary requires a model item of type
  'System.Collections.Generic.IEnumerable