This question already has answers here:
Is there a way to “extract” the type of TypeScript interface property?
                                
                                    (2个答案)
                                
                        
                上个月关闭。
            
        

我想从这样定义的接口中获取“ postType”属性

export interface PostType {
  postType: {
    title?: string;
    content?: string;
  };
}


我要取出并使用“ postType”属性的目的如下

const fn = (post: Post) => {
  ...
}


我使用了“ Pick”,但这是行不通的。

export type Post = Pick<PostType, 'postType'>;


我配置上面接口的原因是PostType实际上是与另一个接口相对应的属性。

因此,我必须遵循PostType的界面。

我怎样才能做到这一点?

最佳答案

如果我理解正确,您想这样做:PostType["postType"]

关于javascript - 如何从 typescript 中获取接口(interface)的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60548398/

10-09 21:22