Typescript Pick - 选择属性字段

Lpp*_*Edd 2 typescript

假设我有一个界面,例如:

interface MyInterface {
   myProperty: {
     one: number
     two: string
   }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能Pick myProperty字段?这可能吗?
想要的结果应该是:

{
   one: number
   two: string
}
Run Code Online (Sandbox Code Playgroud)

这样在使用类型时:

type MyType = ...

const t: MyType = ...
t.one = ...
Run Code Online (Sandbox Code Playgroud)

Tit*_*mir 5

如果您想获取成员的类型,您只需要使用类型查询:

interface MyInterface {
    myProperty: {
        one: number
        two: string
    }
}

type MyType = MyInterface['myProperty']

const t: MyType = {
    one: 1,
    two: '2'
};
t.one = 3
Run Code Online (Sandbox Code Playgroud)

尽管如果可能的话,按照另一个答案的建议重构为单独的类型可能是更明智的方法。