uuidv5的Typescript声明,同时导出枚举和默认函数

我试图为uuidv5创build一个Typescript声明,这是我第一个第三方模块的声明,他们使用的构造我不明白。 脱节的模块如下所示:

function uuidToString(uuid) { } function uuidFromString(uuid) { } function createUUIDv5(namespace, name, binary) { } createUUIDv5.uuidToString = uuidToString; createUUIDv5.uuidFromString = uuidFromString; module.exports = createUUIDv5; 

我试图创build一个这样的声明:

 declare module uuidv5 { type uuid = string | Buffer enum space { dns, url, oid, x500, null, default } type ns = uuid | space export interface createUUIDv5 { (namespace: ns, name: uuid): uuid; (namespace: ns, name: uuid, binary: boolean): uuid; uuidToString(uuid: Buffer): string; uuidFromString(uuid: string): Buffer; createUUIDv5: uuidv5.createUUIDv5; space: uuidv5.space; } } declare const exp: uuidv5.createUUIDv5; export = exp; 

这几乎得到了我想要的,除了我不能访问空间枚举使用的事实

 var uuidNs = uuidv5(uuidv5.spaces.null, "My Space", true); ------------------ var uuid = uuidv5(uuidNs, "My Space", true); 

我经历了文档,但无法find一种方法来在那里添加枚举,同时仍然能够使用它在顶部的types定义…

 declare module uuidv5 { type uuid = string | Buffer enum space { dns, url, oid, x500, null, default } type ns = uuid | space export interface createUUIDv5 { (namespace: ns, name: uuid): uuid; (namespace: ns, name: uuid, binary: boolean): uuid; uuidToString(uuid: Buffer): string; uuidFromString(uuid: string): Buffer; createUUIDv5: uuidv5.createUUIDv5; spaces: typeof uuidv5.space; // notice this line } } declare const exp: uuidv5.createUUIDv5; export = exp; 

我不推荐使用declare module uuidv5格式,因为它已经被弃用了。 ES6模块兼容的环境模块更好。

 declare module 'uuidv5' { type uuid = string | Buffer enum space { dns, url, oid, x500, null, default } type ns = uuid | space interface createUUIDv5 { (namespace: ns, name: uuid): uuid; (namespace: ns, name: uuid, binary: boolean): uuid; uuidToString(uuid: Buffer): string; uuidFromString(uuid: string): Buffer; createUUIDv5: createUUIDv5; spaces: typeof space; } var exp: createUUIDv5 export = exp } 

正在使用:

 import * as uuidv5 from 'uuidv5' var uuidNs = uuidv5(uuidv5.spaces.null, "My Space", true);