Вложенные вызовы функции generi c не компилируются без конкретной оболочки - PullRequest
0 голосов
/ 23 апреля 2020

из a, b и c ниже, я не могу передать a в качестве аргумента d. Это заставляет меня использовать либо конкретный тип, либо, обернуть тип generi c в конкретный тип.

import { createAction, ActionCreatorWithPayload } from '@reduxjs/toolkit';

interface Wrapper<T> {
    inner: T
};

const a = <T>(name: string) => createAction<T>(name);
const b = <T>(name: string) => createAction<Wrapper<T>>(name);
const c = (name: string) => createAction<number>(name);
const d = <T>(action: ActionCreatorWithPayload<T>) => {};
const e = <T>() => {
    d(a<T>("x")); // ERROR - see below
    d(b<T>("x"));
    d(c("x"));
};

Ошибка:

Argument of type 'IsAny<T, ActionCreatorWithPayload<any, string>, IsUnknown<T, ActionCreatorWithNonInferrablePayload<string>, IfVoid<...>>>' is not assignable to parameter of type 'ActionCreatorWithPayload<T, string>'.
  Type 'ActionCreatorWithPayload<any, string> | IsUnknown<T, ActionCreatorWithNonInferrablePayload<string>, IfVoid<...>>' is not assignable to type 'ActionCreatorWithPayload<T, string>'.
    Type 'IsUnknown<T, ActionCreatorWithNonInferrablePayload<string>, IfVoid<T, ActionCreatorWithoutPayload<string>, ActionCreatorWithOptionalPayload<...>>>' is not assignable to type 'ActionCreatorWithPayload<T, string>'.
      Type 'IfVoid<T, ActionCreatorWithoutPayload<string>, ActionCreatorWithOptionalPayload<T, string>> | IsAny<...>' is not assignable to type 'ActionCreatorWithPayload<T, string>'.
        Type 'IsAny<T, IfVoid<T, ActionCreatorWithoutPayload<string>, ActionCreatorWithOptionalPayload<T, string>>, ActionCreatorWithNonInferrablePayload<...>>' is not assignable to type 'ActionCreatorWithPayload<T, string>'.
          Type 'ActionCreatorWithNonInferrablePayload<string> | IfVoid<T, ActionCreatorWithoutPayload<string>, ActionCreatorWithOptionalPayload<...>>' is not assignable to type 'ActionCreatorWithPayload<T, string>'.
            Type 'ActionCreatorWithNonInferrablePayload<string>' is not assignable to type 'ActionCreatorWithPayload<T, string>'.
              Types of property 'match' are incompatible.
                Type '(action: Action<unknown>) => action is { payload: unknown; type: string; }' is not assignable to type '(action: Action<unknown>) => action is { payload: T; type: string; }'.
                  Type predicate 'action is { payload: unknown; type: string; }' is not assignable to 'action is { payload: T; type: string; }'.
                    Type '{ payload: unknown; type: string; }' is not assignable to type '{ payload: T; type: string; }'.
                      Types of property 'payload' are incompatible.
                        Type 'unknown' is not assignable to type 'T'.
                          'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.

Это происходит с --strictFunctionTypes и похоже на этот вопрос , но все же я не могу определить, что можно сделать, чтобы успокоить проверку типов за пределами обходных путей;

  • // @ts-ignore
  • Упаковка в конкретный тип
  • Отключение strictFunctionTypes

Как я могу помочь компилятору вывести правильный тип здесь?

...