V
V
Vladimir2021-08-20 13:05:51
typescript
Vladimir, 2021-08-20 13:05:51

How to declare an Object in which an arbitrary set of fields, but all fields are Array?

There is such an object

const obj = {
  field1: [],
  field2: [],
  somefield: ['one']
}

The bottom line is that the set and the names of the fields in it can be arbitrary, but all fields must be arrays.
How can I declare it in TypeScript?
While obj: any comes to mind but I don't want to do that

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-08-20
@Casufi

You need a Record type

const obj: Record<string, string[]> = {
  field1: [],
  field2: [],
  somefield: ['one'],
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question