Typescript Node.js应用程序中的GUID(/uuid)

tyky79it  于 2022-11-26  发布在  TypeScript
关注(0)|答案(5)|浏览(122)

我尝试在Node/Typescript应用程序中使用uuid(v 3.0.1)包,但我不确定应该导入什么以及如何使用它。
这是index.d.ts(来自@types/uuid版本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

我在这里找不到汇出的类别。
例如,angular2-uuidindex.d.ts看起来如下:

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

很明显可以用途:

let id = UUID.UUID();

那么.如何使用(导入和调用)uuid呢?

bis0qfac

bis0qfac1#

是的,下面是我的项目代码:

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

注意:要安装定义,需要运行

npm install --save-dev @types/uuid
42fyovps

42fyovps3#

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

u59ebvdq4#

这也适用于我:

import uuidv4 from 'uuid/v4'

this.projectId = uuidv4()
ix0qys7i

ix0qys7i5#

我按以下顺序为项目使用uuid

  • 必须安装 *
npm i --save-dev @types/uuid
npm install uuid
  • 使用uuid套件 *
import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);

相关问题