简体   繁体   中英

Advanced JSDoc (TypeScript)

Primary problem: I need type extending any object structure with the concrete object structure.

Default I tested in VS Code.

My solution proposal:

/** @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

Problem resulting from my solution proposal:

/** @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;

This problem is not exists here:

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

In TypeScript is behavior equals.

your code help me to found a way for my issue. Not sure if we have same issue, but here my code. Basically adding Partial< code > remove assignable alert for me: :)

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM