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

Using Redux Sagas with Typescript 4.2 & above #2251

Closed
yaldram opened this issue Feb 10, 2022 · 7 comments
Closed

Using Redux Sagas with Typescript 4.2 & above #2251

yaldram opened this issue Feb 10, 2022 · 7 comments

Comments

@yaldram
Copy link

yaldram commented Feb 10, 2022

Description

We just updated typescript to typescript version 4.2 and we are getting a lot of errors. I read these following resources but these were very basic -

I need some help from the community how should I type a saga with multiple yield calls like in the below file

Please help, thanks a lot.

@JabbyPanda
Copy link

JabbyPanda commented Feb 23, 2022

You can try out typed-redux-saga that will provide better Typescript typings for redux-saga

@neurosnap
Copy link
Member

https://github.com/appsmithorg/appsmith/blob/release/app/client/src/sagas/EvaluationsSaga.ts on line 95 function *evaluateTreeSaga(), it contains multiple yield calls.

Typing multiple yield with different values is an inherent limitation with typescript and generators.

There is an outstanding issue discussing this in-depth: microsoft/TypeScript#43632

Typing redux-saga generators will at best help suppress compiler errors.

For all those yield select I would just cast the values you want.

const allActionValidationConfig: TheExpectedValue = yield select(getAllActionValidationConfig);

For the generator return type, there is a redux-saga type that I use:

import { SagaIterator } from 'redux-saga';

// where RT is the return type
function* evaluateTreeSaga(): SagaIterator<RT> {
  // ...
}

@yaldram
Copy link
Author

yaldram commented Feb 24, 2022

Thanks a lot for your time and inputs @neurosnap @JabbyPanda. I did use the SagaIterator type, but at the end we need to type the yield statements -

function *validateResponse(): SagaIterator<boolean>{}

// It throws an error saying yield returns any
const isValidResponse = yield validateResponse();

// I need to do the following
const isValidResponse: boolean = yield validateResponse();

Not that important but I had to type :boolean everywhere.

@neurosnap
Copy link
Member

If you have anymore follow up questions, please feel free to ask!

@kirgy
Copy link

kirgy commented Aug 5, 2022

Sorry to ressurect this issue @neurosnap, but I have the same problem @yaldram mentioned.

Using this as the return type of the saga:
SagaIterator<PasswordGrantToken>

Appears to have no effect on the return type from the caller:

// below is an `any` type
const newPasswordGrantToken = yield call(requireNewPasswordGrantToken);

See below for typescript error. I'm using redux-saga version 1.1.3 with typescript version 4.6.3

CleanShot 2022-08-05 at 16 48 20@2x

@yaldram
Copy link
Author

yaldram commented Aug 5, 2022

@kirgy yes you have to manually type the yields unfortunately. We are doing the same in our code base.

@neurosnap
Copy link
Member

That's right. Also, that error in particular is being raised because the saga that requireNewPasswordGrantToken is in needs the SagaIterator<any> return type.

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

No branches or pull requests

4 participants