繁体   English   中英

uuidv5 的打字稿声明,导出枚举和默认函数

[英]Typescript declaration for uuidv5, export both enum and default function

我正在尝试为 uuidv5 创建一个 Typescript 声明,这是我对 3rd 方模块的第一个声明,他们使用的是我不理解的构造。 脱衣服的模块看起来像:

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;

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM