typescript - 从对象字面量派生 TypeScript 接口(interface)

标签 typescript

TypeScript 是否能够从对象字面量推断出接口(interface)?就像下面这样:

export const theme = {
  primaryColor: "#e9e9eb",
  secondaryColor: "blue"
};

接口(interface)如下(AFAICT),TypeScript 迫使我为其创建一个单独的接口(interface),但我想自动派生。

export interface IThemeInterface {
  primaryColor: string;
  secondaryColor: string;
}

最佳答案

type IThemeInterface = typeof theme; 在这里做了正确的事情。

关于typescript - 从对象字面量派生 TypeScript 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119073/

相关文章:

node.js - 导入带有导出枚举的模块时找不到模块错误

typescript - 在 TypeScript 中创建 HTMLHeadingElement

javascript - Angular 2 : String interpolation with function call

javascript - 函数返回值中的部分类型不如预期的那么严格

reactjs - React-Localization setLanguage() 不更新页面

javascript - 在 TypeScript 中定义 js-data 资源

reactjs - 如何使用 React Function Components 显示/隐藏微调器、 snackbar 或其他 transient 组件

html - Angular 中的数据列表

TypeScript 目标 ES3

javascript - 为什么不能使用 VueJS 在 vue.config.js 文件中使用 ES 导入?