【问题标题】:JSDoc - Correct way to document a method which potentially returns any type?JSDoc - 记录可能返回任何类型的方法的正确方法?
【发布时间】:2023-03-14 01:25:01
【问题描述】:

我有一个实用函数,它从你的数组中返回一个值。但是,您的数组显然可以包含任何类型的元素,那么我如何记录返回类型是混合的呢?

在 PHPdoc 中我会使用:

@return mixed

JSDoc 等价物是什么?

【问题讨论】:

    标签: javascript jsdoc


    【解决方案1】:

    您可以在 JSDoc 中使用星号

    @returns {*}
    

    或者,如果你知道所有可能的类型,你可以用管道分隔它们

    @returns {String|Number|Boolean}
    

    【讨论】:

      【解决方案2】:

      JSDoc 文档中提到了type expressions

      您可以使用任何 Google Closure Compiler 类型表达式...

      linked page on the Closure Compiler docs 说:

      ALL类型{*}表示该变量可以取任何类型。

      因此,您可以将@returns {*} 用于可以返回任何类型的函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-21
        • 2014-08-04
        • 2013-12-13
        • 2015-02-05
        • 2015-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        相关资源
        最近更新 更多