type a dictionary of arrays in typescript

To type a dictionary of arrays, you can use an indexed type query in TypeScript. Here is an example:

type DictionaryOfArrays<T> = {
  [key: string]: T[];
};

// Example usage:
const myDict: DictionaryOfArrays<number> = {
  'a': [1, 2, 3],
  'b': [4, 5, 6],
  'c': [7, 8, 9],
};
177 chars
11 lines

In this example, the DictionaryOfArrays type alias takes a generic type parameter T. The type alias defines an object type with string keys, and each key maps to an array of type T.

You can then use this type alias to declare a variable like myDict that is a dictionary of number arrays. In this example, we've assigned some example values to the dictionary just for demonstration purposes.

Note that TypeScript also includes a built-in Record type for defining dictionaries. You could use Record<string, T[]> to achieve the same result as the DictionaryOfArrays type alias.

gistlibby LogSnag