I need to document a function that returns an object with the same structure user passed, but fields types are different.
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 }
}
Then someone can use a function like this:
const myThings = makeSomeThings({
thingsA: {
cat: 1,
dog: 2,
},
thingsB: {
bone: 3,
apple: 4,
},
worldName: 'Pretty strange place',
})
myThings autocomplete everything from the user payload, however, some internals of the objects now has a different type.
myThings.thingsA.cat.happyLittleMethod()
// ^
// how can I autocomplete this happyLittleMethod?