我正在尝试为 js 库编写打字稿定义文件。该库导出以下项目:
const Joi = require('joi');
module.exports = {
Joi
};
Joi 是一个流行的库,并且已经定义了类型。(https://www.npmjs.com/package/@types/joi)
如何在不重新定义定义文件中的命名空间的情况下正确导出定义文件中预先存在的 Joi 类型?
这样做的正确方法是什么?
我使用了工具 dts-gen ( https://github.com/Microsoft/dts-gen ) 来了解一下。它为 Joi 创建一个命名空间,如下所示:
export namespace Joi {
const isImmutable: boolean;
const isJoi: boolean;
const schemaType: string;
const version: string;
function allow(...args: any[]): void;
function alt(args: any): any;
function alternatives(args: any): any;
function any(args: any): any;
function applyFunctionToChildren(...args: any[]): void;
function array(args: any): any;
function assert(value: any, schema: any, message: any): void;
function attempt(value: any, schema: any, message: any): any;
function binary(args: any): any;
function bool(args: any): any;
function checkOptions(...args: any[]): void;
function clone(...args: any[]): void;
function compile(schema: any): any;
function concat(...args: any[]): void;
function createError(...args: any[]): void;
function createOverrideError(...args: any[]): void;
function date(args: any): any;
function defaults(fn: any): any;
function describe(args: any): any;
function description(...args: any[]): void;
function disallow(...args: any[]): void;
function empty(...args: any[]): void;
function equal(...args: any[]): void;
function error(...args: any[]): void;
function example(...args: any[]): void;
function exist(...args: any[]): void;
function extend(args: any): any;
function forbidden(...args: any[]): void;
function func(args: any): any;
function invalid(...args: any[]): void;
function isRef(ref: any): any;
function label(...args: any[]): void;
function lazy(fn: any): any;
function meta(...args: any[]): void;
function not(...args: any[]): void;
function notes(...args: any[]): void;
function number(args: any): any;
function object(args: any): any;
function only(...args: any[]): void;
function optional(...args: any[]): void;
function options(...args: any[]): void;
function raw(...args: any[]): void;
function reach(schema: any, path: any): any;
function ref(args: any): any;
function required(...args: any[]): void;
function strict(...args: any[]): void;
function string(args: any): any;
function strip(...args: any[]): void;
function tags(...args: any[]): void;
function unit(...args: any[]): void;
function valid(...args: any[]): void;
function validate(value: any, args: any): any;
function when(...args: any[]): void;
namespace alt {
const prototype: {
};
}
namespace alternatives {
const prototype: {
};
}
namespace any {
const prototype: {
};
}
namespace array {
const prototype: {
};
}
namespace assert {
const prototype: {
};
}
namespace attempt {
const prototype: {
};
}
namespace binary {
const prototype: {
};
}
namespace bool {
const prototype: {
};
}
namespace compile {
const prototype: {
};
}
namespace date {
const prototype: {
};
}
namespace defaults {
const prototype: {
};
}
namespace describe {
const prototype: {
};
}
namespace extend {
const prototype: {
};
}
namespace extensionSchema {
const isImmutable: boolean;
const isJoi: boolean;
const schemaType: string;
function allow(...args: any[]): void;
function and(...args: any[]): void;
function applyFunctionToChildren(...args: any[]): void;
function assert(...args: any[]): void;
function checkOptions(...args: any[]): void;
function clone(...args: any[]): void;
function concat(...args: any[]): void;
function createError(...args: any[]): void;
function createOverrideError(...args: any[]): void;
function describe(...args: any[]): void;
function description(...args: any[]): void;
function disallow(...args: any[]): void;
function empty(...args: any[]): void;
function equal(...args: any[]): void;
function error(...args: any[]): void;
function example(...args: any[]): void;
function exist(...args: any[]): void;
function forbidden(...args: any[]): void;
function forbiddenKeys(...args: any[]): void;
function invalid(...args: any[]): void;
function keys(...args: any[]): void;
function label(...args: any[]): void;
function length(...args: any[]): void;
function max(...args: any[]): void;
function meta(...args: any[]): void;
function min(...args: any[]): void;
function nand(...args: any[]): void;
function not(...args: any[]): void;
function notes(...args: any[]): void;
function only(...args: any[]): void;
function optional(...args: any[]): void;
function optionalKeys(...args: any[]): void;
function options(...args: any[]): void;
function or(...args: any[]): void;
function pattern(...args: any[]): void;
function raw(...args: any[]): void;
function rename(...args: any[]): void;
function required(...args: any[]): void;
function requiredKeys(...args: any[]): void;
function schema(...args: any[]): void;
function strict(...args: any[]): void;
function strip(...args: any[]): void;
function tags(...args: any[]): void;
function type(...args: any[]): void;
function unit(...args: any[]): void;
function unknown(...args: any[]): void;
function valid(...args: any[]): void;
function validate(...args: any[]): void;
function when(...args: any[]): void;
function without(...args: any[]): void;
function xor(...args: any[]): void;
}
namespace extensionsSchema {
const isImmutable: boolean;
const isJoi: boolean;
const schemaType: string;
function allow(...args: any[]): void;
function applyFunctionToChildren(...args: any[]): void;
function checkOptions(...args: any[]): void;
function clone(...args: any[]): void;
function concat(...args: any[]): void;
function createError(...args: any[]): void;
function createOverrideError(...args: any[]): void;
function describe(...args: any[]): void;
function description(...args: any[]): void;
function disallow(...args: any[]): void;
function empty(...args: any[]): void;
function equal(...args: any[]): void;
function error(...args: any[]): void;
function example(...args: any[]): void;
function exist(...args: any[]): void;
function forbidden(...args: any[]): void;
function invalid(...args: any[]): void;
function items(...args: any[]): void;
function label(...args: any[]): void;
function length(...args: any[]): void;
function max(...args: any[]): void;
function meta(...args: any[]): void;
function min(...args: any[]): void;
function not(...args: any[]): void;
function notes(...args: any[]): void;
function only(...args: any[]): void;
function optional(...args: any[]): void;
function options(...args: any[]): void;
function ordered(...args: any[]): void;
function raw(...args: any[]): void;
function required(...args: any[]): void;
function single(...args: any[]): void;
function sparse(...args: any[]): void;
function strict(...args: any[]): void;
function strip(...args: any[]): void;
function tags(...args: any[]): void;
function unique(...args: any[]): void;
function unit(...args: any[]): void;
function valid(...args: any[]): void;
function validate(...args: any[]): void;
function when(...args: any[]): void;
}
namespace func {
const prototype: {
};
}
namespace isRef {
const prototype: {
};
}
namespace lazy {
const prototype: {
};
}
namespace number {
const prototype: {
};
}
namespace object {
const prototype: {
};
}
namespace reach {
const prototype: {
};
}
namespace ref {
const prototype: {
};
}
namespace string {
const prototype: {
};
}
namespace validate {
const prototype: {
};
}
}
然而,这对我来说似乎不正确。当实际定义已经存在时,我不明白 Joi 需要命名空间。
谢谢