Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!