问题
I'm having a problem with typing,
particularly with the reduce()
function, where accumulator[currentValue]
returns an error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'CombinedState<{ order: TOrderState; employee: TEmployeeState; }>'. No index signature with a parameter of type 'string' was found on type 'CombinedState<{ order: TOrderState; employee: TEmployeeState; }>'.
interface TArgs {
path: string[]
action: Record<string, string>
}
const testFunc = ({ path, action }: TArgs): Promise<string> => {
return new Promise(resolve => {
const unsubscribe = store.subscribe(() => {
const state = store.getState()
const value = path.reduce(
(accumulator, currentValue) => accumulator[currentValue], state)
if (value) {
resolve(value)
unsubscribe()
}
})
store.dispatch(action)
})
}
testFunc(["order", "partials", "details"], {type: "SOME_ACTION"})
In the end value
returns a "string", as the result of accessing state's nested property.
How do I type this correctly?
来源:https://stackoverflow.com/questions/62812472/type-nested-property-state-in-array-reduce-redux-typescript