Typescript如何提取嵌套类型

时间:2019-06-26 18:36:27

标签: typescript

如何提取嵌套属性的类型?例如说我有这种类型:

type Example = {
   nested: string,  // how do I infer string here
   other: string
}

这样我可以从Example.nested中提取“字符串”吗?

我有type myType = Pick<Example, "nested">,并且提供了{ nested: string },但是我想推断该对象上“嵌套”属性的类型(在此示例中为字符串)。

1 个答案:

答案 0 :(得分:2)

您要使用使用方括号语法的lookup type(也称为“索引访问类型”)。

type myType = Example["nested"] // string

希望有所帮助;祝你好运!

Link to code