Skip to content

Rewrite "Usage with TS" guide page #4042

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

Merged
merged 10 commits into from
Mar 19, 2021
Merged

Rewrite "Usage with TS" guide page #4042

merged 10 commits into from
Mar 19, 2021

Conversation

markerikson
Copy link
Contributor

@markerikson markerikson commented Mar 19, 2021


name: 📖 New/Updated Documentation Content
about: Adding a new docs page, or updating content in an existing docs page

PR Type

Does this PR add a new page, or update an existing page?

Updates the "Usage with TypeScript" page

Checklist

What docs page is being added or updated?

  • Section: Recipes
  • Page: Usage with TypeScript

For Updating Existing Content

  • Dropped legacy section that showed use of action type unions andhand-written action creators
  • Copied TS quick start section from RTK docs
  • Added "what you'll learn / prerequisites" boxes
  • Added additional details for typing reducers / thunks / middleware
  • Removed now-duplicate RTK usage info
  • Added additional RTK common usage patterns info
  • Added "recommendations" section

Also added a new Style Guide entry that says to "organize state by data type, not components"

- Dropped legacy section that showed use of action type unions and
  hand-written action creators
- Copied TS quick start section from RTK docs
- Added "what you'll learn / prerequisites" boxes
- Added additional details for typing reducers / thunks / middleware
- Removed now-duplicate RTK usage info
- Added additional RTK common usage patterns info
- Added "recommendations" section
@github-actions
Copy link

github-actions bot commented Mar 19, 2021

Size Change: 0 B

Total Size: 8.9 kB

ℹ️ View Unchanged
Filename Size Change
dist/redux.js 6.37 kB 0 B
dist/redux.min.js 2.53 kB 0 B

compressed-size-action

@netlify
Copy link

netlify bot commented Mar 19, 2021

Deploy preview for redux-docs ready!

Built with commit 077cf97

https://deploy-preview-4042--redux-docs.netlify.app

@markerikson markerikson changed the title Add style guide entry on state structure by data typeDocs/ts guide updates Rewrite "Usage with TS" guide page Mar 19, 2021
Copy link
Member

@msutkowski msutkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits only. Content looks 💯


### Use the React-Redux Hooks API

**We recommend using the React-Redux hooks API as the default approach**. The hooks API is much simpler to use with TypeScript, as `useSelector` is a simple hook that takes a selector function, and the return type is easily inferred from the type of the `state` argument.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note here, some edits have been set to React Redux, while others are React-Redux as it is here. Not a deal, but thought I'd point it out.

@markerikson markerikson merged commit 5b8938c into master Mar 19, 2021
@markerikson markerikson deleted the docs/ts-guide-updates branch March 19, 2021 22:54
Comment on lines +113 to +115
const initialState: CounterState = {
value: 0
}
Copy link
Member

@phryneas phryneas Mar 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small curveball - see reduxjs/redux-toolkit#827 (maybe also add a short explanation as to why we're doing it that way from there)

- const initialState: CounterState = {
+ const initialState = {
  value: 0
- }
+ } as CounterState

export const DELETE_MESSAGE = 'DELETE_MESSAGE'
```ts title="app/hooks.ts"
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import { RootState, AppDispatch } from './store'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using
import type { RootState, AppDispatch } from './store'
might help some bundlers that work on a per-file level without looking at the type of the imported token and otherwise introduce a runtime dependency to ./store


Type checked chat reducer:
If you are using Redux Toolkit's `createSlice`, you should rarely need to specifically type a reducer separately. If you do actually write a standalone reducer, it's typically sufficient to declare the type of the `initialState` value, and type the `action` as `AnyAction<string>`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnyAction does not accept any arguments, so it's just AnyAction, not AnyAction<string>

default:
return state
}
action: AnyAction<string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- action: AnyAction<string>
+ action: AnyAction

}
```ts
export interface Middleware<
DispatchExt = {}, // legacy type parameter that is no longer relevant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DispatchExt is what we use extensively in redux toolkit.
It is used to add additional function overloads ("extensions") to the resulting store.dispatch


export type RootState = ReturnType<typeof rootReducer>
export const exampleMiddleware: Middleware<
{}, // legacy type parameter added to satisfy interface signature
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import { Middleware } from 'redux'
You will typically want to provide the `R` (return type) and `S` (state) generic arguments. Unfortunately, TS does not allow only providing _some_ generic arguments, so the usual values for the other arguments are `unknown` for `E` and `Action<string>` for `A`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally prefer AnyAction over Action<string> because it seems to convey the intent a bit more, but that's just my nitpick.

additionalMiddleware,
// you can also type middlewares manually
untypedMiddleware as Middleware<
(action: Action<'specialAction'>) => number,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's that "legacy" parameter in action ;)

webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
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

Successfully merging this pull request may close these issues.

3 participants