Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Declare allowable paramer of a function as all possible keys of an object in JSDoc

The goal

I have an object which is basically a constant map of key-value pairs:

const mailTemplates = {
    "email-verification": "some-api-key",
    "welcome": "another-key",
    "password-reset": "lorem-ipsum"
};

I have another function which looks like this:

const sendMail = (type, data) => {
   const templateId = mailTemplates[type];
   
   // rest of function logic
}

I want the first argument (type) to be JSDoc-ed so that it only shows possible keys from the map above (in intellisense).

Things I've tried

  1. Obviously, I can manually assign them like this (and I'm doing this currently):
/**
 * @param {"email-verification" | "welcome" | "password-reset"} type
 */

But as the code grows, more key-value pairs will be added and that'll be two things to manage. I'm looking for a more dynamic solution.

  1. This doesn't work:
/**
 * @param {Object.keys(mailTemplates).join(" | ")} type
 */
  1. Neither does this:
/**
 * @typedef {string} keys
 */
const TEMPLATE_KEYS = Object.keys(mailTemplates).join(" | ")
/**
 * @param {keys} type
 */
  1. I even tried with enums as per this answer but there was no intellisense.

P.S.

  1. This is more likely to be a case of premature optimization (of maintainability) but I'd still like to know for future references.

  2. Yes, the join(" | ") probably won't cut it and it require some more transformations but I wanted to check if JS expressions work inside JSDoc first.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda