How to document a string type in jsdoc with limited possible values

前端 未结 5 395
春和景丽
春和景丽 2020-12-02 15:08

I am having a function that accepts one string parameter. This parameter can have only one of a few defined possible values. What is the best way to document the same? Shoul

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 15:29

    This is how the Closure Compiler supports it: you can use "@enum" to define a restricted type. You don't actually have to define the values in enum definition. For instance, I might define an "integer" type like:

    /** @enum {number} */
    var Int = {};
    
    /** @return {Int} */
    function toInt(val) {
      return /** @type {Int} */ (val|0);
    }
    

    Int is generally assignable to "number" (it is a number) but "number" is not assignable to "Int" without some coercion (a cast).

提交回复
热议问题