Typescript: Generate Type from Object keys and Array string values

Viewed 2175

I'm looking to generate a type from an Object's keys, and values of arrays of strings. The type needs to represent all the possible strings, i.e.

const Actions = {
  foo: ['bar', 'baz'],
}

# type generated from Actions to equal:
type ActionsType = 'foo' | 'bar' | 'baz'

I need to retain Actions as it's to be passed to a method, therefore:

const Actions = {
  foo: ['bar', 'baz'],
} as const


type ActionsType = keyof typeof Actions | typeof Actions[keyof typeof Actions][number]

whilst generating the type correctly didn't allow me to pass Actions to a method expecting Record<string, string[]> as the keys and values became readonly.

How can I generate the required type whilst still being able to use Actions as a non-readonly Object?

1 Answers

Here is a simple solution to revert readonlyness of const assertions / as const:

type Mutable<T> = {-readonly [K in keyof T]: Mutable<T[K]>}
The following will compile with a type assertion and adequate type safety:
type T1 = Mutable<typeof Actions> // { foo: ["bar", "baz"]; }

function foo(a: Record<string, string[]>) {}
foo(Actions as Mutable<typeof Actions>) // works

Be aware, that Actions object might be mutated inside foo, so you better treat it as mutable in the containing module as well. A cleaner alternative would be to change foo signature to receive readonly arrays:

function foo2(a: Record<string, readonly string[]>) {}
foo2(Actions) // works without type assertion

Code demo

Related