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).
/**
* @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.
/**
* @param {Object.keys(mailTemplates).join(" | ")} type
*/
/**
* @typedef {string} keys
*/
const TEMPLATE_KEYS = Object.keys(mailTemplates).join(" | ")
/**
* @param {keys} type
*/
enums as per this answer but there was no intellisense.This is more likely to be a case of premature optimization (of maintainability) but I'd still like to know for future references.
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.