In the context of a state reducer presented as follows:
const anObject = {
fruit: 'Apple',
today: new Date(),
}
function reducer(state, stateReducer) {
return stateReducer(state);
}
const fruit = reducer(anObject, state => state.fruit);
// It is expected that fruit will be of type string.
const now = reducer(anObject, state => state.now);
// It is expected that now will be of type Date.
I am interested in using Typescript generics to ensure that the output state from
reducer(anObject, state => state.someState)
matches the correct type depending on the data it returns. How can this goal be accomplished? Thank you for your assistance.
An interactive demonstration of the code above can be accessed via this link: https://codesandbox.io/s/adoring-booth-jlnhn