javascript - 高级 JSDoc( typescript )

标签 javascript jsdoc

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

我在 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 中,行为是相等的。

最佳答案

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

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

enter image description here

关于javascript - 高级 JSDoc( typescript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62403326/

相关文章:

javascript - 如何在 Internet Explorer 9 中使用 JSON.parse 和 json2.js?

javascript - 如果子 div 为空的 html,则隐藏父 div

javascript - 如何在 tsc 从 .js 文件生成的类型定义中包含继承的属性?

comments - TypeScript 的文档工具,例如 JSDoc

typescript - 在 TypeScript 和/或 JSDoc 中,如何指示记录类型中的某些属性名称是同一类型中同级属性的别名?

javascript - ReactJS + 终极版 : How to structure action creators down to each component?

javascript - Angular UI 路由器 : where should I be changing state? Controller ?服务?事件监听器?

javascript - 使用来自不同异步函数的响应创建一个 json 对象

带有节点的 jsdoc 生成空的 index.html

javascript - AngularJS类类型检查解决方案