2 ms·
Since TS is just JS, you could just dispatch to arbitrary strings, but the TS type system isn't going to help you out very much. The approach the author is taki
by nirvdrum 7y ago
Since TS is just JS, you could just dispatch to arbitrary strings, but the TS type system isn't going to help you out very much. The approach the author is taking is the one described in the Redux documentation [0].
[0] -- https://redux.js.org/recipes/usage-with-typescript https://redux.js.org/recipes/usage-with-typescript
- acemarke 7y agoHi, I'm a Redux maintainer. FWIW, there's apparently a ton of ways to handle typing Redux with TS (and that's even reflected in the discussion for the PR that added that page [0]). A large part of it seems to revolve around whether or not you're trying to apply action types to `dispatch` to limit what actions can be dispatched. I'm comfortable with TS at a general level (declaring interfaces for API types, reducers, and React component props), but a lot of folks seem to go crazy trying to get 100% type coverage. I personally aim for more of an "80%" sweet spot for typing - trying to trade off overall benefit with not trying to tie myself in knots adding ridiculous type declarations. I actually just wrote a tutorial page for Redux Starter Kit that shows how to use it with TS and `useDispatch` [1], but had someone point out that one of the examples I was using for thunks was losing a bit of type safety [2]. This stuff is hard :) [0] https://github.com/reduxjs/redux/pull/3201 https://github.com/reduxjs/redux/pull/3201 [1] https://redux-starter-kit.js.org/tutorials/advanced-tutorial https://redux-starter-kit.js.org/tutorials/advanced-tutorial [2] https://github.com/markerikson/rsk-github-issues-example/pull/1 https://github.com/markerikson/rsk-github-issues-example/pul...
- nirvdrum 7y agoThanks for the feedback. The earliest examples I had see of using redux just passed strings around and struck me as being error-prone. So I was happy to see const fields used as the action types. The TypeScript approach looks reasonable to me, if not verbose. But it looks like there's no real consensus on what the best practice is. Certainly some food for thought. Thanks!