uuidv5的typescript声明,导出枚举和默认函数

时间:2017-01-14 20:08:12

标签: node.js typescript node-modules

我试图为uuidv5创建一个打字稿声明,这是我第一次宣布第三方模块并且他们正在使用我不理解的构造。脱衣模块看起来像:

url: "http://10.1.1.1/packages/10amd64-default"

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

function uuidToString(uuid) {
}

function uuidFromString(uuid) {
}

function createUUIDv5(namespace, name, binary) {
}

createUUIDv5.uuidToString = uuidToString;
createUUIDv5.uuidFromString = uuidFromString;

module.exports = createUUIDv5;

几乎得到了我想要的东西,除了我无法使用

访问空间枚举
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;

我浏览了文档,但是找不到在那里添加枚举的方法,同时仍然可以将它用于顶部的类型定义......

1 个答案:

答案 0 :(得分:2)

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);