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

WIP: Static Types doc page #1439

Merged
merged 6 commits into from
Nov 23, 2019
Merged

WIP: Static Types doc page #1439

merged 6 commits into from
Nov 23, 2019

Conversation

JNaftali
Copy link
Contributor

Static types doc page - see #1001 (comment)

@JNaftali
Copy link
Contributor Author

Explanations definitely need fleshing out and polishing. Uploading as a WIP to get feedback. Any feedback would be appreciated but primarily looking for:
(a) Document structure - do I need more sections? Should the existing ones be in a different order?
(b) Examples - Are they detailed enough/cover enough use cases? Are they clear enough?

@netlify
Copy link

netlify bot commented Oct 30, 2019

Deploy preview for react-redux-docs ready!

Built with commit 3142336

https://deploy-preview-1439--react-redux-docs.netlify.com

@JNaftali
Copy link
Contributor Author

Also: couldn't figure out how to get the page working with docusaurus, so I didn't do anything in that area 😬 .

Next thing I'm gonna do is read #1336 - if that's getting finished up part of me wants to wait for that to be merged in and then integrate what I'm writing into that.


# Type safety and React-Redux

The [`react-redux` type definitions](https://npm.im/@types/react-redux) export some helpers to make it easier to write typesafe interfaces between your redux store and your React components.
Copy link
Contributor

Choose a reason for hiding this comment

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

Jumping straight into this sentence feels odd. Seems like there needs to be more of an introduction here.

I'd suggest rephrasing this section to say that React-Redux doesn't include its own types, and that you need to install @types/react-redux first.

Also, the "helpers" bit really only applies to connect, so let's move that phrasing later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You don't think the TypedUseSelectorHook type counts?

id: static-types
title: Type safety and React-Redux
hide_title: true
sidebar_label: Type safety and React-Redux
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe retitle this to "Static Typing"?

If you manually type your selector functions, `useSelector` will return the same type as your selector

```ts
// selectIsOn always returns a boolean, so isOn is typed as a boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

Show the definition of selectIsOn first.


## Typing the useSelector hook

If you manually type your selector functions, `useSelector` will return the same type as your selector
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe reword this as "If your selector function has declared types, ..."

const isOn = useSelector((state: MyStateType) => state.isOn)
```

If you'd like, you can define a typed `useSelect` hook using a helper type
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefix with "If you want to avoid repeating the state type declaration, you can..."

isOn: boolean
}

const connector = connect(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd want to highlight that normally we do the connect(mapState, mapDispatch)(MyComponent) double-function-call all in one step at the end, but for this to work we break it up into two steps (and particularly doing the first part before the component definition) so we can infer the props from the connector function.

The return type of `ConnectedProps` can then be used to type your props object.

```tsx
interface Props extends PropsFromRedux {
Copy link
Contributor

Choose a reason for hiding this comment

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

Y'know, I keep forgetting you can extend interfaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you rather I say type Props = PropsFromRedux & {? I've a slight preference for types, but the codebase I work on mostly uses interfaces. I've no idea which would be easier to understand

Copy link
Contributor

Choose a reason for hiding this comment

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

That's the syntax I've been using, but not sure if it matters that much.

backgroundColor: string
}

export const MyComponent = connector((props: Props) => (
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's separate out the component definition just for clarity.

))
```

If you are using untyped selectors or for some other reason would rather, you can also pass type arguments to `connect` and use those same types to create your own props type.
Copy link
Contributor

Choose a reason for hiding this comment

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

"would rather".... what?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"would rather not rephrase an awkwardly worded sentence"


type Props = StateProps & DispatchProps & OwnProps

const connector = connect<
Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, so this shows manually typing connect, but we should show using Props explicitly for the component when it's declared separately. (Feel free to omit the actual rendering logic here.)

@markerikson
Copy link
Contributor

Paging @wgao19 . Thoughts on the existing content? Can you do something similar for the Flow part?

@JNaftali
Copy link
Contributor Author

JNaftali commented Nov 9, 2019

Revised some things according to your feedback, @markerikson. In addition to the little things, I swapped the order of the last section around and added a bit about type hoisting. Not sure if that bits necesssary

@markerikson
Copy link
Contributor

markerikson commented Nov 23, 2019

@wgao19 @endiliey @yangshun The only way I can get this to show up in the main sidebar is if I explicitly put the page into versioned-docs/v7.1, and use a specific versioned ID. Any idea why just adding it to /docs and sidebar.json doesn't get it to show up in the v7.1 version?

I have to say that I find the versioning stuff very confusing. Maybe it's harder because we're treating v7.1 as our "current" version - I'd kinda like changes to /docs on master to show up there, rather than having master show up as a next version.

@markerikson
Copy link
Contributor

In any case, I'm happy with the way this looks now, so I'll merge it. I'd still like to see if there's a better way to add new pages with the versioning, though.

@markerikson markerikson merged commit f059ad0 into reduxjs:master Nov 23, 2019
@markerikson
Copy link
Contributor

Thanks to @JNaftali for getting this put together!

albertodev7 pushed a commit to albertodev7/react-redux that referenced this pull request Dec 8, 2022
* first draft of new page on static types

* revised because feedback

* mention that we're splitting up the export and connect call

* Update "Static Types" content

* Add "Static Typing" page to sidebar

* Move Static Typing page to get it to show up in 7.1 sidebar
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.

2 participants