Alex Ironside
Alex Ironside

Reputation: 5039

Multiple wildcard types keys

I have this data:

{

    name1: 'name',
    name2: [{},{},{}],
    name3: true,
}

And I want to type it. Name1, name2, and name3 are created automatically, and may have other names, but the structure is like this.

How do I type this?

I tried doing smth like this:

export type data = {
  [key: string]: boolean;
  [key: string]: number;
  [key: string]: Array<MachineMapping>;
};

But now I'm getting:

Duplicate index signature for type 'string'.

How do I handle this?

Upvotes: 0

Views: 83

Answers (1)

joshvito
joshvito

Reputation: 1563

You can use a union.

export type data = {
  [key: string]: boolean | number | Array<MachineMapping>;
};

Upvotes: 1

Related Questions