javascripttypescriptvisual-studio-codejsdoc.d.ts

JSDoc multiple key value pair generic


How can I add multiple key value pairs to a function call similar to the way document.createElement works (<K extends keyof HTMLElementTagNameMap>(tagName: K): HTMLElementTagNameMap[K])

This is only so that the JSDoc is properly setup and VSCode intellisense picks up on it and works appropriately.

Map declaration:

export interface KeyMap {
  "A": "true" | "false";
  "B": "a" | "b" | "c";
  "C": "foo" | "bar";
}

Function declaration:

export interface Options {
  [key: keyof KeyMap]: KeyMap[key];
  // It's not working since I don't have a way to refer to the generic type of the key
}

export function test(options: Options);

Calling the function

test({ "A": "true", "C": "bar" });

Since it's on a key and value basis, I don't know how to add a generic so I can later get the possible values for each key.


Solution

  • I fixed it by using { [K in keyof KeyMap]: KeyMap[K]; } and using a type instead of an interface

    export type Options = {
      [K in keyof KeyMap]: KeyMap[K];
    }