javascript - 如何从 typescript 获取接口(interface)的属性

标签 javascript typescript

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

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

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

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

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

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

我配置上面接口(interface)的原因是PostType实际上是一个对应于另一个接口(interface)的属性。

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

我该怎么做?

最佳答案

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

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

相关文章:

javascript - 在 Select2 MultiSelect 下拉列表中添加自定义标签样式

javascript - 将鼠标悬停在上下文菜单的 li 元素上时,如何更改它的颜色

typescript - 如何配置 VS Code 自动导入?

Angular 单元测试 :WARN: 'Navigation triggered outside Angular zone, did you forget to call ' ngZone. 运行()'?'

在 Web Worker 中导入 tensorflow 时出现 Angular typescript 类型检查问题

javascript - Chrome 扩展选项卡捕获 API 音频流以在 HTML 页面中播放

javascript - 使用 filter() 删除属性键

javascript - Angular 2中的日期选择器问题

typescript - Vue、TypeScript 无法调用组件方法

javascript - 如何在 typescript 中使用外部js