-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
Typing Effect Arguments for adding listeners is difficult at the moment. #2114
Comments
Hmm. For reference, the type we have for that is called The bigger issue here is that it isn't even defined as a union of objects or anything. It's specifically a set of function overloads, with varying techniques for figuring out what the I'll be honest and say I don't necessarily see a lot of a syntactic benefit to defining the listeners as an array and doing For now, I think the best option is to use the approach shown in the "counter" example in the repo: export function setupCounterListeners(
startListening: AppStartListening
): Unsubscribe {
const subscriptions = [
startListening({
actionCreator: counterActions.updateByPeriodically,
effect: onUpdateByPeriodically,
}),
startListening({
actionCreator: counterActions.updateByAsync,
effect: onUpdateAsync,
}),
]
return () => {
subscriptions.forEach((unsubscribe) => unsubscribe())
}
} Some similarity, but the difference is that the function exported by the slice accepts the pre-typed |
Awesome thanks I'll give this a go. Is this pattern well documented outside of the example repo? It'd be rough if people went with a different pattern in javascript that worked but was hard to use when converting to typescript. |
No, we haven't actually documented any usage patterns for how to organize listeners yet. But, this question has come up 2-3 times now, so it might be worth adding to the docs. |
Also is there an example of typing this correctly with the extra parameter you can configure with the middleware I did
which seems to be working. |
We didn't particularly anticipate people using |
Oh got it
works well I'll go with that |
Resolved with pattern described in examples no action needed. Thanks @markerikson for the explanation. |
We'd like to structure our listener effects like so, colocating them in our slice file.
we'd then import them and add them to our stores middleware
This is proving very difficult though because getting access to the type parameters passed to startListening is impossible at the moment.
I've tried to derive the argument like so
But I'm unable to do so because Parameters won't derive overloaded arguments microsoft/TypeScript#26591. Is there anyway an explicit AddListenerOptions type be exported so I can create a well typed AppListener?
The text was updated successfully, but these errors were encountered: