2017-05-07 56 views
9

我試圖在節點/打字稿應用一個uuid(V 3.0.1)包的工作,但我不知道我應該怎麼導入以及如何使用它。GUID/UUID在打字稿的Node.js應用

這是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.tsangular2-uuid看起來像這樣:

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

而且它很明顯的使用方法:

let id = UUID.UUID(); 

所以。如何使用(導入和呼叫)uuid

回答

16

是的,這裏是代碼我的項目:

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

或剛剛從 'uuid是''{進口V4};' 這是解決我的問題。謝謝! – tBlabs