Typescript 如何从嵌套对象中拾取类型?

Typescript 如何从嵌套对象中拾取类型?,typescript,types,Typescript,Types,我有一个与年级相关的类型学生(一个学生可以有多个年级): 导出接口学生{ 身份证号码:, 名称:string 姓氏:string 年龄:数目 电子邮件:string 职系:名单 } 我只想拾取List类型的对象的键,为此我编写了一个类型: export type Filter<T, Condition> = { // Set all types that match the Condition to the value of the field (i.e. name: "n

我有一个与年级相关的类型学生(一个学生可以有多个年级):

导出接口学生{
身份证号码:,
名称:string
姓氏:string
年龄:数目
电子邮件:string
职系:名单
}
我只想拾取
List
类型的对象的键,为此我编写了一个类型:

export type Filter<T, Condition> = {
    // Set all types that match the Condition to the value of the field (i.e. name: "name")
    // Else set the type to never
    [K in keyof T]: T[K] extends Condition ? K : never 
}[keyof T] // Selects all the types of all the keys except for never

导出类型筛选器={
//将与条件匹配的所有类型设置为字段值(即名称:“名称”)
//否则将类型设置为“从不”
[K in keyof T]:T[K]扩展条件?K:从不
}[keyof T]//选择除never之外的所有键的所有类型
过滤器
生成类型:
“等级”

下一步是从
“Grades”
获取类型
Grade

我尝试了以下方法:

Student[“Grades”]
但这会导致类型
列表

是否有办法只获取内部类型?

您可以使用它来获取泛型类型的泛型参数,如下所示:

type ListType<T> = T extends List<infer U> ? U : never;
let grade: ListType<Student["Grades"]> // inferred type Grade
let grade: ListType<Student[Filter<Student, List<any>>]> // inferred type Grade

这正是我所需要的。如果
列表是一个
数组
,你可以选择
学生['Grades'][number]
我试过了,但列表不是
数组
它是一个客户链接列表。我不得不用它,因为这是一个家庭作业问题
let grade: ListType<Student[Filter<Student, List<any>>]> // inferred type Grade