define type to equal one of the interface properties

Lets assume we have an interface:

interface Foo {
  aaa: string
  bbb: SomeType
  ccc: SomeOtherType
  // ... and 100 more
}

I would like to define a type, such that it can receive any of the Foo interface property types.

Obviously I could do it manually:

type Bar = Foo['aaa'] | Foo['bbb'] // etc.

But that is impractical and not DRY, especially with large interfaces. Is there any better way to define such type, so that it automatically accepts interfaces types?

Upvotes: 0

Views: 622

Answers (1)

Mahdi Ghajary
Mahdi Ghajary

Reputation: 3253

You can achieve what you want using mapped types:

type Bar = Foo[keyof Foo]

Playground.

Upvotes: 1

Related Questions