0

How do i extract the type of key3 in MyInterface2 for use in key3Value similar to key2Value?

interface MyInterface {
    key1: {
        key2: string
    }
}

const key2Value: MyInterface['key1']['key2'] = 'Hi' //Works fine

interface MyInterface2 {
    key1: {
        key2: Array<{ key3: string }>
    }
}

const key3Value: MyInterface2['key1']['key2']['key3'] = 'Hi' //Property 'key3' does not exist on type '{ key3: string; }[]'.(2339)

Link to typescript playground.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
Ivo Ivanov
  • 41
  • 1
  • 3
  • `MyInterface2['key1']['key2']` is an array, so you would need to index it like `const key3Value: MyInterface2['key1']['key2'][0]['key3'] = 'Hi'` – MjZac Nov 14 '19 at 12:28
  • Related (though not dupe): https://stackoverflow.com/questions/46376468/how-to-get-type-of-array-type-values – tevemadar Nov 14 '19 at 12:29

1 Answers1

0

You have to add an index to get into the array:

const key3Value: MyInterface2['key1']['key2'][0]['key3'] = 'Hi'