如何在接口的成员内部提取数组成员的类型



如何在MyInterface2中提取key3的类型,以便在类似于key2Value的key3Value中使用?

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)

链接到打字游戏场。

您必须添加一个索引才能进入数组:

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

最新更新