Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
How to describe with JSDoc a function that returns the same structured object as takes in argument, but some properties will have a different type?

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?
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda