Necesito documentar una función que devuelve un objeto con la misma estructura que pasó el usuario, pero los tipos de campos son diferentes.
class CoolThing { constructor (id, settings) { } happyLittleMethod () { /* do something */ } } /** * This definition says that an object returns ALMOST the same structure that passed. * Feels close. * * @template T * @param {T} ABThingsAndStuff * @returns {T} */ function makeSomeThings ({ thingsA, thingsB, ...otherStuff }) { const a = {} const b = {} for (let key in thingsA) { a[key] = new CoolThing(key, thingsA[key]) } for (let key in thingsB) { b[key] = new CoolThing(key, thingsB[key]) } return { thingsA: a, thingsB: b, ...otherStuff } }Entonces alguien puede usar una función como esta:
const myThings = makeSomeThings({ thingsA: { cat: 1, dog: 2, }, thingsB: { bone: 3, apple: 4, }, worldName: 'Pretty strange place', }) myThings autocompleta todo, desde la carga útil del usuario, sin embargo, algunas partes internas de los objetos ahora tienen un tipo diferente.
myThings.thingsA.cat.happyLittleMethod() // ^ // how can I autocomplete this happyLittleMethod?