CNIS
CNIS

Reputation: 121

How to specify the following type in typescript?

I want to create an interface in which we have a variable named sortedTabs. The variable sortedTabs is of type object which can have arbitrary many arrays in it but every array has the same type tabsAn[] but how would I specify that type? How would I tell TypeScript that I expect an object which holds an arbitrary number of arrays of type tabsAn[]?

export interface Initialization{
    sortedTabs: // how to specify the object described above?
}

Upvotes: 0

Views: 86

Answers (1)

Aplet123
Aplet123

Reputation: 35512

There are two ways, the first being with an index type:

export interface Initialization {
    sortedTabs: { [key: string]: tabsAn[] }
}

The second being with the Record utility type, which is really just a wrapper around index types:

export interface Initialization {
    sortedTabs: Record<string, tabsAn[]>
}

Upvotes: 1

Related Questions