[英]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.我尝试使uuid
(v 3.0.1) package 在 Node/Typescript 应用程序中工作,但我不确定应该导入什么以及如何使用它。
This is index.d.ts
(from @types/uuid
v 2.0.29):这是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
I cant find exported class here.我在这里找不到导出的 class。
For example index.d.ts
from angular2-uuid
looks like that:例如index.d.ts
angular2-uuid
的 index.d.ts 看起来像这样:
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
?如何使用(导入和调用) uuid
?
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
Per the tests:根据测试:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/uuid/uuid-tests.ts https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/uuid/uuid-tests.ts
import uuid = require('uuid');
let uuidv4: string = uuid.v4();
import * as uuid from "uuid";
const id: string = uuid.v4();
This also works for me:这也适用于我:
import uuidv4 from 'uuid/v4'
this.projectId = uuidv4()
I used uuid for my project in the following order我按以下顺序为我的项目使用 uuid
Must be installed必须安装
npm i --save-dev @types/uuid
npm install uuid
Using the uuid package使用 uuid package
import { v4 as uuidv4 } from 'uuid';
const myuuid = uuidv4();
console.log('Your UUID is: 'myuuid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.