Dawid Krajewski
Dawid Krajewski

Reputation: 364

TypeScript: force interface to have all of the indexes of union type

I have union like:

type A = "a" | "b" | "c";

And I would like to have an interface like:

interface B {
  [index: A]: string
}

But I would want this interface to force to have all of the options of union type, so the final interface would look like this:

interface B {
  a: string;
  b: string;
  c: string;
}

Is it possible to achieve?

Upvotes: 0

Views: 120

Answers (1)

Dmitriy
Dmitriy

Reputation: 2822

You can achieve this using mapped type:

type B = {
  [K in A]: string
};

Upvotes: 1

Related Questions