Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected typescript type error with expectSaga #378

Open
Yupeng-li opened this issue Dec 2, 2021 · 0 comments
Open

Unexpected typescript type error with expectSaga #378

Yupeng-li opened this issue Dec 2, 2021 · 0 comments

Comments

@Yupeng-li
Copy link

Not all the generator functions pass the type check when using expectSaga. Sometimes unexpected type error is raised if the yields in the function are mixed by saga effects and normal generator functions.

// Example
import { expectSaga } from "redux-saga-test-plan";
import { select } from "redux-saga/effects";

function* dummyGenerator(name: string) {
  yield select();
  console.log(name);
}

function* dummySagaFn() {
  const name: string = yield select(); // Type the return value. Important to reproduce the issue
  yield dummyGenerator(name);
}

it("", async () => {
  expectSaga(dummySagaFn).withState({ name: "test-name" }).run(); // type error 
});

Sample code (line 15): https://codesandbox.io/s/cranky-resonance-47uuz?file=/src/demo.test.ts
Typescript version: 4.5

// Source code

interface Iterator<T, TReturn = any, TNext = undefined> {
    // NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
    next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
    return?(value?: TReturn): IteratorResult<T, TReturn>;
    throw?(e?: any): IteratorResult<T, TReturn>;
}

interface IterableIterator<T> extends Iterator<T> {
    [Symbol.iterator](): IterableIterator<T>;
}

export type SagaType = (...params: any[]) => SagaIterator | IterableIterator<any>;

export const expectSaga: (<S extends SagaType>(
  generator: S,
  ...sagaArgs: Parameters<S>
) => ExpectApi) & { DEFAULT_TIMEOUT: number };

The issue is in SagaType. IterableIterator<any> extends Iterator<T>. The TNext is default to undefined. If we type one of the return values of yield statements (line 10 in the example), the error occurs.

Potential solution:
Change SagaType to export type SagaType = (...params: any[]) => SagaIterator | Generator<any>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant