Ali Faris
Ali Faris

Reputation: 18592

how define new type based on interface with some modifications

I have this interface

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

now I want to define object its properties look like this

 [KeyName] : () => [TypeOfKey]

for example FooReducer

ReducerFoo : () => ReducerFooState

so the type of my object should be like this

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

can I do something like this without the need to define another interface that describes my object?

I need this because the Store interface will be evolved as my app grow and I don't want to change two thing any time I add a new property to the Store interface

Upvotes: 1

Views: 29

Answers (1)

david4
david4

Reputation: 61

Try using a mapped type:

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

When you add new properties to the Store interface, this mapped type doesn't need to be changed.

Upvotes: 1

Related Questions