jfairbank / redux-saga-test-plan

Test Redux Saga with an easy plan.
http://redux-saga-test-plan.jeremyfairbank.com
MIT License
1.25k stars 127 forks source link

Fixes inferred args type of expectSaga and testSaga #387

Closed lourd closed 2 years ago

lourd commented 2 years ago

This makes a small tweak to how the parameters for a saga given to expectSaga or testSaga are inferred.

This snippet does not work with the latest version, 4.0.4 and TypeScript 4.6

function* helperSaga<T>(foo: T, saga: (item: T) => any) {
  yield saga(channel);
}

function* handler(num: number) {
  yield num;
}

expectSaga(helperSaga, 42, handler);

The TypeScript compiler has an error on the last line:

function handler(num: number): Generator<number, void, unknown>
Argument of type '(num: number) => Generator<number, void, unknown>' is not assignable to parameter of type '(item: unknown) => any'.
  Types of parameters 'num' and 'item' are incompatible.
    Type 'unknown' is not assignable to type 'number'.ts(2345)

By making the type for the saga's params inferred as a generic type parameter and passing that through to SagaType as a type parameter, the compiler infers them correctly and the error is resolved.

jp928 commented 2 years ago

@lourd Many thanks for your PR.