1

Adding anything other than generic types doesn't type check on key-map objects

Example:

/**
 * @typedef {"MARKET1"|"MARKET2"|"MARKET3"} Market
 * @typedef {{
    name: string,
   }} Item

  this type checks just items, not keys
 * @typedef {{
    items: Object.<string, Item[]>,
   }} Config
   
  this doesn't type check anything, even items
  having a non generic type in the key field doesn't raise any error
 * @typedef {{
    items: Object.<Market, Item[]>
   }} Config  
*/
2
  • Why are you using JSDoc typedefs with typescript Commented Mar 15, 2022 at 13:46
  • it is a small project so no need for project-wise typescript. i am only using JSDoc for validating a config file. typescript looks a bit overkill. Commented Mar 15, 2022 at 15:20

1 Answer 1

1

Just use the utility type Record instead of Object.<key, value>. It's going to be like the snippet below:

/**
 * @typedef {"MARKET1"|"MARKET2"|"MARKET3"} Market
 * @typedef {{
    name: string,
   }} Item
 */

/**
 * @typedef {{
  items: Record<string, Item[]>,
 }} Config1
 */

/**
 * @typedef {{
  items: Record<Market, Item[]>
 }} Config2
 */

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.