【问题标题】:Advanced JSDoc (TypeScript)高级 JSDoc (TypeScript)
【发布时间】:2020-11-27 20:30:21
【问题描述】:

主要问题:我需要使用具体对象结构扩展任何对象结构的类型。

我在 VS Code 中测试的默认值。

我的解决方案建议:

/** @template A @typedef {{[Ki in keyof A]:       A[Ki] } & {[k: string]:       {} } & A} Deep3 */
/** @template A @typedef {{[Ki in keyof A]: Deep3<A[Ki]>} & {[k: string]: Deep3<{}>} & A} Deep2 */
/** @template A @typedef {{[Ki in keyof A]: Deep2<A[Ki]>} & {[k: string]: Deep2<{}>} & A} Deep */

/** @type {Deep<{a: {b: number}}>} */
let x = {a: {b: 9, c: {d: 8}}};     // ERROR: Type 'number' is not assignable to type '{ [k: string]: {}; }'
x.a.c.d = 9;    // OK

我的解决方案引发的问题:

/** @type {Number & {[k: string]: Number}} */
let a = 9;          // ERROR: Type '11' is not assignable to type '{ [k: string]: { x: number; }; }'
a.optional = 9;     // OK

/** @type {Number | {[k: string]: Number}} */
let b = 9;          // OK
b.optional = 9;     // ERROR: Property 'optional' does not exist on type 'number'
b = 4;

这里不存在这个问题:

/** @type {Number & {optional?: Number}} */
let c = 9;          // OK
c.optional = 9;     // OK

在 TypeScript 中,行为等于。

【问题讨论】:

    标签: javascript jsdoc


    【解决方案1】:

    您的代码帮助我找到解决问题的方法。 不确定我们是否有同样的问题,但这里是我的代码。 基本上为我添加Partial&lt; code &gt; 删除可分配警报! :)

    /**@type  { Partial<{[Ki in keyof A]: A[Ki] }> } */
    

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 2020-11-17
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2021-02-10
      • 2018-12-22
      • 2020-02-21
      相关资源
      最近更新 更多