-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[Typescript] Yield return value type safety #884
Comments
I believe this is a Typescript limitation, so I filed an issue on their end: microsoft/TypeScript#14883. |
facing the same issue now that I'm trying to adopt Typescript. |
@raduflp There's no workaround since it's lacking support at the language level. There are several threads mentioned in the issue above that discuss this feature, saying that it was blocked on boolean literal types and default template parameters (both now implemented), but generator yield type propagation still hasn't seen any traction in the past year. The closest thing to a workaround is still what I wrote in the code sample above: you can manually assert the type yourself to make sure the rest of the code is typesafe, but there's nothing you can do to automatically detect if that original type-assertion is wrong. |
It seems that typescript 3.6 will finally bring support for strongly typed iterators: microsoft/TypeScript#31639 |
@nicgirault TS@3.6 will supposedly come with better support for generators, so this is probably something which we'll tackle then. |
Hi @Andarist Is there any in-progress task due to this issue? or help how I can make PR for that? |
@PejmanNik this is happening in #1892 |
Thank you @Andarist 👍 |
@PejmanNik I don't want to disappoint you, but note that my PR won't be sufficient to fix this issue properly. Saga generators would indeed be strongly typed, yet yielded effects return types would still be any as in the OP example. As far as I know, TS 3.6 still won't be able to make TNext depend on what is yielded. However, I have a PoC running locally proving that it should be possible to make a ESLint rule which would assert that:
I'll work on this ESLint rule when #1892 gets merged if I have some time. Sure having proper type inference built at the language level would be way better, but such ESLint rule would at least prevent most mistakes I think. |
Thank you @gilbsgilbs for your great work. |
@PejmanNik Well, I'm not sure to follow. Do you have a code example? As far as I know, in this case: function* gen0() {
yield* [1, 2, 3];
return 'foo';
}
function* gen1() {
const result = yield* gen0();
}
|
oh sorry @gilbsgilbs. I think this is crazy way but at last it work function* callSafe<Fn extends (...args: any[]) => Generator>(fn: Fn, ...args: Parameters<Fn>)
{
const result = yield call(fn,...args);
return result as Parameters<ReturnType<Fn>["return"]>[0];
}
function* typeTest ()
{
const result = yield* callSafe(fn);
}
I'm on Ts |
This is very hackish, yet it makes sense and I think your approach is correct (typings-wise) and isn't a bug in TypeScript. I didn't realize Maybe this is something we could include in the TypeScript recipe or even in a third-party library 🤔 . Well done. Edit: It may be quite easy to confuse though (in similar scenarios to the one described in #1286 (comment) section "Generic types and Sagas"). We'll have to test this out. |
Thank you @gilbsgilbs If you and other maintainers of saga think it can help. I can work on PR or a small third-party library. |
I'm not a maintainer of Redux-Saga 😉 . Just a happy user who is looking for improved type-safety and trying my best to contribute towards this direction. Since it's a TS-specific hack, I don't think it has its place in the main repo. |
so many of your comments help me. Thanks for having TS user back 😄 |
@PejmanNik I just threw this together, it might serve as a starting point. |
@PejmanNik @danielnixon has kindly done the work to get the library you proposed started. As of this writing typed-redux-saga is an active repo with 11 commits, and redux-saga-safetype has a single commit with an empty README.md. He didn't steal your idea, he's giving you a foundation to build on. Why don't you help out instead of condemning him contributing to the community? |
Shouldn't this be part of this library rather than a separate library? |
Return typings worked for me with @danielnixon's typed-redux-saga library but it would be great to get this integrated into the main lib. |
Hey guys, three years since the topic was started... or may I have an example when it works good? |
Four years in 25 days already, @egorovsa ! But that's not fair, we should start counting at least from the release of TS 3.6 - which is 1.5 years. I would like to thank everybody trying to have it done, guys. It was funny to discover that |
|
This is a serious issue. I spent a whole day to fix this. I added a * just next to yield as per typescript suggestion but my whole saga stopped working. One way is to disable typescript chekc strcit mode off untill this is fixed |
@saswat3115 Honestly, I ended up just putting dozens of As for the stars — that can only work with https://github.com/agiledigital/typed-redux-saga , as the underlying stuff need to be changed too. But it didn't work for me as well because I was going to use its Macro mode and that didn't work for some reason. |
I've been using typed-redux-saga in production for two years now, and I haven't had any problems with it at all. I highly recommend switching instead of bending over backwards trying to make redux-saga type safe. |
@parkerault Sounds encouraging! Do you use starred yields or macro? |
I started using it before the macro was available, so I've always used yield*. There are some cases where I've had to fall back to the untyped effects for reasons that I can't remember off the top of my head, which isn't ideal since every team member needs to understand the difference between the two libraries. However, the benefits still outweigh the drawbacks in my opinion. Regardless, I haven't encountered any bugs or unexpected behavior while using typed-redux-saga in production. |
I installed typed-redux-saga, and I get the following TS error message upon compilation :
Not sure what to do with this. I guess I will do what @OnkelTem did. I will add |
@harvey56 did you try to restart webpack? |
@harvey56 are you compiling from within VS Code? if you started getting an error when you upgraded VS code, it may be the version of typescript that it uses internally that's causing the problem. The typed-redux-saga repo has |
Use the response type of your api . For me, I am using Apisauce. You can refer below code.
|
Hi there, I was crazy with this thing before. Then I found that If I add an interface for responce..., that warning was gone. interface IResponse {
data?: any;
}
export function* getStreets(payload: any) {
const variables = payload.payload;
const response: IResponse = yield call(services.getStreets, variables);
const { getStreetByProvinceAndDistrict } = response?.data?.result || {};
// ......
} |
That will work, but the problem with doing it that way is if someone changes the signature of |
redux-saga/redux-saga#884 TODO: Migrate to typed-redux-saga?
I'm ramping up on redux-saga and Typescript by working on a small project.
Something I've noticed is that the return value of
yield effect(...)
is alwaysany
. I'm not familiar enough with the platform to understand if this is a limitation of Typescript, or some missing type annotation in redux-saga/my code.E.g.:
The text was updated successfully, but these errors were encountered: