简体   繁体   English

我如何像 lodash 一样使用 JSDoc 对参数进行分组?

[英]how do I group parameters with JSDoc like lodash?

I want to know how lodash can group suggestions for parameters on the functions they have based on the type or number of parameters that users enter as shown below:我想知道lodash如何根据用户输入的参数类型或数量对它们所具有的函数的参数建议进行分组,如下所示:

在此处输入图片说明

in the picture above, it can be seen that there are 8 groupings of suggestions on the each function in lodash .在上图中,可以看到lodash 中each函数有8 组建议。 and below is a sample code in my case:以下是我的示例代码:

Example function:示例函数:

function filter(allowSize, max) {
  //do Something
}


If filter function just have 1 parameter, use jsdoc below:如果filter函数只有 1 个参数,请使用下面的 jsdoc:

/**
 * @param {object} allowSize
 * @param {number} allowSize.min
 * @param {number} allowSize.max
 */

I want to remove max parameter from suggestion, but I don't know how to do it我想从建议中删除max参数,但我不知道该怎么做



If filter function have 2 parameter, use jsdoc below:如果filter函数有 2 个参数,请使用下面的 jsdoc:

/**
 * @param {number} allowSize
 * @param {number} max
 */

I want to rename allowSize parameter to min in suggestion, but I don't know how to do it我想在建议allowSize参数重命名为min ,但我不知道该怎么做


how do I do the above using jsdoc ?我如何使用jsdoc执行上述操作?

You can use function overload method, but javascript does not support this method.Then how do you do the lodash ?可以使用函数重载的方法,但是javascript不支持这种方法,那么lodash怎么做呢?

the answer is lodash using typescript to write their code.Typescript allows you to write functions using the overload method.Typescript will generate a file with the extension .d.ts which contains information related to the code written.答案是lodash使用 typescript 编写代码。Typescript 允许您使用重载方法编写函数。Typescript 将生成一个扩展名为.d.ts的文件,其中包含与所编写代码相关的信息。

Example:例子:

function filter(allowSize: object): any;
function filter(min: number, max: number): any;
function filter(arg1: object | number, arg2?: number): any {
    if (typeof arg1 === "object") {
        // do something when the function only have 1 parameter
    } else if (typeof arg1 === "number" && typeof arg2 === "number") {
        // do something when the function has 2 parameters
    } else {
        // default
    }
}

after that, compile the TS file with the command tsc -d typescript_file.ts之后,使用命令tsc -d typescript_file.ts编译 TS 文件

the -d parameter is useful for telling the compiler to create a declaration file. -d参数对于告诉编译器创建声明文件很有用。

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

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