如何从打字稿中获取接口的属性

时间:2020-03-05 15:08:07

标签: javascript typescript

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

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

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

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

我使用了“ Pick”,但这不起作用。

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

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

所以我必须遵循PostType的界面。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

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