typescript - 如何根据接口定义新类型并进行一些修改
问题描述
我有这个界面
interface Store {
ReducerFoo : ReducerFooState;
ReducerBar : ReducerBarState;
ReducerTest : ReducerTestState;
}
现在我想定义对象它的属性看起来像这样
[KeyName] : () => [TypeOfKey]
例如FooReducer
ReducerFoo : () => ReducerFooState
所以我的对象的类型应该是这样的
{
ReducerFoo : () => ReducerFooState;
ReducerBar : () => ReducerBarState;
ReducerTest : () => ReducerTestState;
}
我可以在不需要定义另一个描述我的对象的接口的情况下做这样的事情吗?
我需要这个,因为Store
界面将随着我的应用程序的增长而发展,并且我不想在任何时候更改两件事,因为我向Store
界面添加新属性
解决方案
尝试使用映射类型:
type StoreFunctions = {
[K in keyof Store]: () => Store[K];
}
当您向 Store 接口添加新属性时,不需要更改此映射类型。
推荐阅读
- jmeter - 使用 CSV 数据集配置元素读取 Excel 文件
- google-apps-marketplace - 如何将营销材料添加到 Google Workspace Marketplace
- javascript - Bootstrap Scrollspy 不起作用 - 缺少什么?
- oracle - 为视图创建唯一键或主键约束
- python - Jobs-Cloud Scheduler (Google Cloud) 无法运行计划的管道
- css - 谷歌浏览器中的插入框阴影视觉工件
- jsf - PrimeFaces FileUpload 测量上传时间
- node.js - Windows Azure App Service 上的错误:请手动安装 sqlite3 包
- html - 使用 *ngIf 和 *ngFor 以角度打开侧边栏菜单
- android - UI 测试 NoMatchingViewException