Type'State'不满足约束'(state:any,…args:any [])=> any'

问题描述 投票:1回答:1

这可能是一个讨厌的问题,但是如何处理Typescript错误TS2344?

类型'状态'不满足约束'(状态:任何,... args:任何[])=>任何'。

这里是我的sagas.ts发生错误的代码片段

function* loadPageFull(action: Actions.LoadPageFullAction ) {
    if (!action.id)
        return;
    const currentPageFull: Interfaces.PageFull = 
      yield (select<Top.State>(Selectors.getPageFull(action.id))); // <-- error occurs here

    if (!currentPageFull || action.forceReload) {
        // here we query the API of the backend and return some JSON
    }
}

问题似乎是与Top.State对齐的yield。奇怪的是,在将Typescript更新到版本3.6.4之前,我没有出现错误。

EDIT: getPageFullselectors.ts中定义为

const getPageFullInner = (state: State, id: number) => state.pagesFull.get(id);
export const getPageFull = (id: number) => (state: Top.State) 
    => getPageFullInner(foobar(state), id);

foobar()功能也在此定义

export const foobar = (state: State) => state.foobar;

参考

reactjs typescript redux-saga
1个回答
1
投票

select的签名是:

export function select<Fn extends (state: any, ...args: any[]) => any>(
  selector: Fn,
  ...args: Tail<Parameters<Fn>>
): SelectEffect

因此,第一个通用参数必须是函数类型(特别是(state: any, ...args: any[]) => any,但是您将其赋予State

您无需指定通用参数,因为可以从参数中推断出该参数,因此您可以简单地编写:

select(Selectors.getPageFull(action.id))
© www.soinside.com 2019 - 2024. All rights reserved.