Основная проблема: мне нужен тип, расширяющий любую структуру объекта с помощью конкретной структуры объекта.
По умолчанию Я тестировал в 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 поведение равно.