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:
in the picture above, it can be seen that there are 8 groupings of suggestions on the each
function in lodash . 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:
/**
* @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
If filter
function have 2 parameter, use jsdoc below:
/**
* @param {number} allowSize
* @param {number} max
*/
I want to rename
allowSize
parameter tomin
in suggestion, but I don't know how to do it
how do I do the above using jsdoc ?
You can use function overload method, but javascript does not support this method.Then how do you do the 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.
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
the -d
parameter is useful for telling the compiler to create a declaration file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.