类型 'R[P]' 不满足约束 '(...args: any) => any'

时间:2021-05-05 07:05:57

标签: typescript redux reselect

我正在尝试将 reselect 和 redux 与 typescript 集成。我正在出口各种类型的减速机。这是我的类型。

type Reducers = {
    user: Reducer<{
        isLogged: boolean;
        isLogging: boolean;
        isFinished: boolean;
        response: {
            idToken: string;
            sessionState: string;
            accessToken: string;
            refreshToken: string;
            tokenType: string;
            scope: string;
            profile: {
                ...;

我想把它转换成这样

State = {
    user: {
        isLogged: boolean;
        isLogging: boolean;
        isFinished: boolean;
        response: {
            idToken: string;
            sessionState: string;
            accessToken: string;
            refreshToken: string;
            tokenType: string;
            scope: string;
            profile: {
                ...;

这是我成功的方法,但我收到打字稿错误。我相信这是因为 ReturnType 应该与 ReturnType<() => R[P]> 之类的箭头函数一起使用,但这并没有给我想要的结果

export type CreateState<R> = {
  [P in keyof R]: ReturnType<R[P]>
}
type State = CreateState<Reducers>

typescript error message image

1 个答案:

答案 0 :(得分:0)

您可以在没有任何额外类型的情况下使其更通用:

export type CreateState<R> = {
    [P in keyof R]: R[P] extends (...args: any) => any ? ReturnType<R[P]> : never
}