【问题标题】:dynamic keys in jsdoc typedefjsdoc typedef中的动态键
【发布时间】:2019-10-07 02:05:53
【问题描述】:

jsdoc typedef 中是否可以有动态键(prop 名称)?我想象这看起来像下面的示例(不起作用)。

@typedef {Object} Foo
@property {string} bar
@property {*} *

传递未在 typedef e.g. {baz: 0} 中列出的属性会使 typescript 出现类似的情况,

'{ bar: string; 类型的参数baz:数字; }' 不可分配给 'Foo' 类型的参数。 对象字面量只能指定已知属性,而“Foo”类型中不存在“baz”


使用@jcalz Object.<string, *> 提出的方法似乎更接近理想的输出但导致了奇怪的输出
@typedef {Object} Foo
@property {number} bar
@property {Object.<string, *>}

输出:

type Foo = {
    bar: number;
    (Missing): {
        [x: string]: any;
    };
}

【问题讨论】:

  • 也许使用 Object.&lt;string, *&gt; 之类的东西而不是 Object,就像提到的 here
  • @jcalz,我无法完成这项工作。我将结果添加到帖子中
  • 对不起,我以前从未真正使用过 JSDoc。我建议用Object.&lt;string, *&gt; 替换第一行中的Object,这样可以添加索引签名,但是当您添加bar 时它会中断。我看到的唯一有效的是/**@typedef { {[k: string]: any, bar: string } } Foo */,但这基本上只是使用 TypeScript 类型语法而不是 JSDoc。

标签: typescript jsdoc


【解决方案1】:

您也可以在 JSDOC 中使用普通的 TS 语法。

看下一个例子:

/**
 * 
 * @param {Record<string, string> & {bar:string}} arg
 */
const foo = (arg) => {}

您甚至可以使用实用程序类型:

/**
 * 
 * @param {Partial<{age:number}>} arg 
 */
const partial = (arg) => { }

您可以找到更多实用程序here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-11
    • 2020-04-17
    • 2017-08-28
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    相关资源
    最近更新 更多