Typescript: how to set type as a key of interface

Viewed 18

Say I have an interface:

interface ExampleInterface {
  someItem: 'a' | 'b' | 'c'
  anotherItem: boolean
  lastItem: string
}

Say I want to set the type in another interface as ExampleInterface.someItem like:

 interface ExampleInterface2 {
  newItem: ExampleInterface.someItem
}

I don't want to create type SomeItem = 'a' | 'b' | 'c' and then set it in both. I'm trying to use an interface in an external typescript library.

is this possible?

2 Answers

I figure it out for anyone with the same issue:

interface ExampleInterface {
  someItem: 'a' | 'b' | 'c'
  anotherItem: boolean
  lastItem: string
}

interface ExampleInterface2 {
  newItem: ExampleInterface['someItem']
}

Yep, this is possible!

 interface ExampleInterface2 {
  newItem: ExampleInterface["someItem"]
}
Related