gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:40:38 25 4
gpt4 key购买 nike

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

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

关于node.js - uuidv5 的 Typescript 声明,同时导出枚举和默认函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41654467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com