Type nested property state in array reduce Redux Typescript

送分小仙女□ 提交于 2021-01-29 08:45:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!