JSDoc:如何定义具有无函数默认值的解构函数参数?

huangapple go评论68阅读模式
英文:

JSDoc: How to define a destructured function parameter with a default value of no function?

问题

给定函数 f({ val1, func1 }),其中 val1 是整数,而 func1 是函数,我该如何创建一个 JSDoc 定义,表明 func1 是一个 可选 函数,并且在函数声明中为该函数分配一个默认值?(目前将 'false' 作为默认值,但 VSCode 假定 func1 参数为布尔型。我需要使用 void 来表示未传递函数吗?)

例如:

function f({ val1 = 0, func1 = false })
/**
 * @description 这是一个执行某些操作的函数。
 * @param {Object} params - 这是函数的参数
 * @param {number} [params.val1=0] - 这是执行某些操作的数字
 * @param {function} [params.func1=false] - 这是执行某些操作的函数
 * @returns {number}
 */
{
    if (val1 != 0)
    {
        // 做些事情
    }
    
    if (func1)
    {
        val1 += func1(val1);
    }
    return val1;
}
英文:

Given function f({ val1, func1 }) where val1 is an integer, and func1 is a function, how can I create a JSDoc definition that indicates func1 is an optional function, as well as assign the function a default value in the function declaration? (Currently assigning 'false' as the default value, but VSCode then assumes the func1 parameter is boolean. Do I need to use void to indicate no function is passed?)

For example:

function f({ val1 = 0, func1 = false })
/**
 * @description This is a function that does something.
 @ @param {Object} params - This is the function's parameters
 * @param {number} [params.val1=0] - This is a number that does something
 * @param {function} [params.func1=false] = This is a function that does something
 * @returns {number}
*/
{
    if (va1 != 0)
    {
        // Do something
    }
    
    if (func1)
    {
        val1 += func1(val1);
    }
    return val1;
}

答案1

得分: 1

你在这里有一些错误。首先,你的 jsdoc 注释应该放在函数声明的上一行。一旦你这样做了,你的 IDE 就会显示你有一些错误,首先是你的代码引用了 va1,而参数是 val1,第二个错误导致了你的问题。

但重点是你将 JavaScript 的假设混合到了你的类型中。IDE 将指出 Type 'boolean' is not assignable to type 'Function',你可以通过将其类型定义为 function|boolean 来轻松修复,但一旦你这样做,你的 if 代码块将假设 func 值是函数或者 true。所以这还不够。

你可以进一步将其定义为特定的 function|false 类型,然后 IDE 知道如果它不是 false,那么它必须是一个函数,并且没问题。然而,在这种基本情况下混合类型可能有些奇怪。你可能实际上想要做的是将函数类型定义为 function|null,然后不要将其默认值设置为 false,而是将其默认值设置为 null,这样它会按照你的期望工作。

最后,我建议你具体地将函数定义为 @param { (arg0: number) => number} [params.func1=() => 0],并将 () => 0 作为默认值。然后,当你尝试传递一个不是整数的值时,IDE 会告诉你,并且知道你可以对该函数进行数学运算。

英文:

You've got a few things wrong here. First your jsdoc comment should go on the line(s) above your function declaration. As soon as you do that, your IDE will show you that you've got a few errors--the first being that your code is referencing va1 whereas the param is val1, the second leading to your question.

But the big thing is you're mixing javascript assumptions into your types. The IDE will point out that Type 'boolean' is not assignable to type 'Function', you can easily fix this by typing it as function|boolean but as soon as you do that then your if block will assume that your func value is either a function or true. So that's not quite it either.

You could go one step further and type it as specifically function|false and then the IDE knows that if it's not false, then it must be a function and be fine. However, mixing types like that for this basic case is weird. What you probably actually want to do is to type your function as function|null and then instead of defaulting it to false, you can then default it to null and it will work like you're expecting.

Lastly what I would instead suggest is to specifically type your function as @param { (arg0: number) => number} [params.func1=() => 0] and have () => 0 be your default value. Then the IDE will tell you when you try to pass a value into it that's not an integer, and knows that you're allowed to do math with that function.

huangapple
  • 本文由 发表于 2023年3月21日 00:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定