【问题标题】:Type 'State' does not satisfy the constraint '(state: any, ...args: any[]) => any'类型 'State' 不满足约束 '(state: any, ...args: any[]) => any'
【发布时间】:2020-02-24 00:33:46
【问题描述】:

这可能是一个菜鸟问题,但是我该如何处理 Typescript 错误 TS2344?

类型 'State' 不满足约束 '(state: any, ...args: any[]) => any'。

这是我的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
    }
}

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

编辑: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】:

    select的签名是:

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

    所以第一个泛型参数必须是函数类型(特别是(state: any, ...args: any[]) =&gt; any),但你给它的是State

    你不需要指定泛型参数,因为它可以从参数中推断出来,所以你可以简单地写:

    select(Selectors.getPageFull(action.id))
    

    【讨论】:

    猜你喜欢
    • 2020-07-07
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多