如何使用TypeScript从npm扩展模块?

ozm*_*ozm 6 node.js typescript joi

我正在使用带有TypeScript的joi@ types/joi.Joi有一个extend方法,它允许通过返回一个新实例来扩展joi,而无需修改原始的joi库.我用它创建了一个扩展实例.

为了这个扩展实例创建的定义,我试图Module Augmentation描述这里使用下面的代码:

declare module 'joi' {
  // Add a new Schema type which has noChildren() method.
  interface CustomSchema extends ObjectSchema {
    noChildren(): this;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,正如预期的那样,这会通过扩充来修改原始定义.我想要的是为扩展实例创建定义,它继承原始内容而不修改它.

还扩展Joi了如下:

import * as Joi from 'joi';
const JoiExtended = Joi.extend({...some implementation...})
// How to export?
// export * from 'Joi' ---> In this case, original non-extended Joi is exported
// export default JoiExtended ---> Imported `Joi` reports: Cannot find namespace 'Joi'
Run Code Online (Sandbox Code Playgroud)
  1. 如何创建扩展定义?
  2. 如何出口扩展Joi

PS我正在学习TypeScript并寻找这个问题的答案但是找不到答案,也许,因为我不习惯使用打字稿术语并搜索错误的术语.

Cut*_*ert 9

Joi.extend返回joi可以使用导出Root类型的模块的新实例.

您需要创建一个扩展的接口,Joi.Root另一个扩展joi您正在扩展的基本类型.您可以joi像导入任何其他对象一样导出自定义实例.

下面是使用API文档示例中的round()dividable()规则的示例.extend()

import * as Joi from 'joi';

interface ExtendedNumberSchema extends Joi.NumberSchema {
    round(): this;
    dividable(num: number): this;
}

interface ExtendedJoi extends Joi.Root {
    number(): ExtendedNumberSchema;
}

const customJoi: ExtendedJoi = Joi.extend((joi) => ({
    base: joi.number(),
    name: 'number',
    language: {
        round: 'needs to be a rounded number', // Used below as 'number.round'
        dividable: 'needs to be dividable by {{q}}'
    },
    pre(value, state, options) {

        if (options.convert && this._flags.round) {
            return Math.round(value); // Change the value
        }

        return value; // Keep the value as it was
    },
    rules: [
        {
            name: 'round',
            setup(params) {

                this._flags.round = true; // Set a flag for later use
            },
            validate(params, value, state, options) {

                if (value % 1 !== 0) {
                    // Generate an error, state and options need to be passed
                    return this.createError('number.round', {v: value}, state, options);
                }

                return value; // Everything is OK
            }
        },
        {
            name: 'dividable',
            params: {
                q: joi.alternatives([joi.number().required(), joi.func().ref()])
            },
            validate(params, value, state, options) {

                if (value % params.q !== 0) {
                    // Generate an error, state and options need to be passed, q is used in the language
                    return this.createError('number.dividable', {v: value, q: params.q}, state, options);
                }

                return value; // Everything is OK
            }
        }
    ]
}));


const schema = {
    a: customJoi.number().round().dividable(3)
};

const result = customJoi.validate({a: 4.1}, schema); // will fail because 4 is no divisible by 3

console.log(result);

export = customJoi;
Run Code Online (Sandbox Code Playgroud)