首页 > 解决方案 > 如何根据接口定义新类型并进行一些修改

问题描述

我有这个界面

interface Store {
    ReducerFoo : ReducerFooState;
    ReducerBar : ReducerBarState;
    ReducerTest : ReducerTestState;
}

现在我想定义对象它的属性看起来像这样

 [KeyName] : () => [TypeOfKey]

例如FooReducer

ReducerFoo : () => ReducerFooState

所以我的对象的类型应该是这样的

{
    ReducerFoo : () => ReducerFooState;
    ReducerBar : () => ReducerBarState;
    ReducerTest : () => ReducerTestState;
}

我可以在不需要定义另一个描述我的对象的接口的情况下做这样的事情吗?

我需要这个,因为Store界面将随着我的应用程序的增长而发展,并且我不想在任何时候更改两件事,因为我向Store界面添加新属性

标签: typescript

解决方案


尝试使用映射类型

type StoreFunctions = {
  [K in keyof Store]: () => Store[K];
}

当您向 Store 接口添加新属性时,不需要更改此映射类型。


推荐阅读