uuidv5 的打字稿声明,同时导出枚举和默认函数
Posted
技术标签:
【中文标题】uuidv5 的打字稿声明,同时导出枚举和默认函数【英文标题】:Typescript declaration for uuidv5, export both enum and default function 【发布时间】:2017-05-29 23:56:30 【问题描述】:我正在尝试为 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);
我浏览了文档,但找不到在其中添加该枚举的方法,同时仍然能够将其用于顶部的类型定义...
【问题讨论】:
您是否尝试从模块中导出space
枚举?
我想我是。我希望能够从调用代码中使用它,现在当我尝试使用它时它不会显示或编译。
【参考方案1】:
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);
【讨论】:
谢谢!那工作得很好。一旦你看到答案,它就会变得如此明显:)。以上是关于uuidv5 的打字稿声明,同时导出枚举和默认函数的主要内容,如果未能解决你的问题,请参考以下文章