"This guide is a living compendium documenting the most important patterns and recipes on how to use React (and its Ecosystem) in a functional style using TypeScript. It will help you make your code completely type-safe while focusing on inferring the types from implementation so there is less noise coming from excessive type annotations and it's easier to write and maintain correct types in the long run."
Found it useful? Want more updates?
Show your support by giving a :star:
:tada: Now updated to support TypeScript v4.6 :tada:
:rocket: _Updated to typesafe-actions@5.x :rocket:
--strict flag) without losing type information downstream through all the layers of our application (e.g. no type assertions or hacking with any type)Check out our Playground Project located in the /playground folder. It contains all source files of the code examples found in the guide. They are all tested with the most recent version of TypeScript and 3rd party type-definitions (like @types/react or @types/react-redux) to ensure the examples are up-to-date and not broken with updated definitions (It's based on create-react-app --typescript).
Playground project was created so that you can simply clone the repository locally and immediately play around with all the component patterns found in the guide. It will help you to learn all the examples from this guide in a real project environment without the need to create complicated environment setup by yourself.
You can help make this project better by contributing. If you're planning to contribute please make sure to check our contributing guide: CONTRIBUTING.md
You can also help by funding issues. Issues like bug fixes or feature requests can be very quickly resolved when funded through the IssueHunt platform.
I highly recommend to add a bounty to the issue that you're waiting for to increase priority and attract contributors willing to work on it.
🌟 - New or updated section
React.FC<Props> | React.FunctionComponent<Props>React.Component<Props, State>React.ComponentType<Props>React.ComponentProps<typeof XXX>React.ReactElement | JSX.ElementReact.ReactNodeReact.CSSPropertiesReact.XXXHTMLAttributes<HTMLXXXElement>React.ReactEventHandler<HTMLXXXElement>React.XXXEvent<HTMLXXXElement>redux-observable
reselectreact-redux
npm i -D @types/react @types/react-dom @types/react-redux
"react" - @types/react
"react-dom" - @types/react-dom
"redux" - (types included with npm package)*
"react-redux" - @types/react-redux
*NB: Guide is based on types for Redux >= v4.x.x.
React.FC<Props> | React.FunctionComponent<Props>Type representing a functional component
const MyComponent: React.FC<Props> = ...
React.Component<Props, State>Type representing a class component
class MyComponent extends React.Component<Props, State> { ...
React.ComponentType<Props>Type representing union of (React.FC<Props> | React.Component<Props>) - used in HOC
const withState =
(
WrappedComponent: React.ComponentType
,
) => { ...
React.ComponentProps<typeof XXX>Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props)
type MyComponentProps = React.ComponentProps<typeof MyComponent>;
React.ReactElement | JSX.ElementType representing a concept of React Element - representation of a native DOM component (e.g. `
), or a user-defined composite component (e.g.`)
const elementOnly: React.ReactElement =
|| <MyComponent />;
React.ReactNodeType representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined ||
|| <MyComponent />;
const Component = ({ children: React.ReactNode }) => ...
React.CSSPropertiesType representing style object in JSX - for css-in-js styles
const styles: React.CSSProperties = { flexDirection: 'row', ...
const element =
`
Type representing HTML attributes of specified HTML Element - for extending HTML Elements
```tsx
const Input: React.FC<Props & React.InputHTMLAttributes<HTMLInputElement>> = props => { ... }
<Input about={...} accept={...} alt={...} ... />
React.ReactEventHandler<HTMLXXXElement>Type representing generic event handler - for declaring event handlers
const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... }
<input onChange={handleChange} ... />
React.XXXEvent<HTMLXXXElement>Type representing more specific event. Some common event examples: ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent.
const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
In code above React.MouseEvent<HTMLDivElement> is type of mouse event, and this event happened on HTMLDivElement
::codeblock='playground/src/components/fc-counter.tsx'::
::codeblock='playground/src/components/fc-counter-with-default-props.tsx'::
::codeblock='playground/src/components/fc-spread-attributes.tsx'::
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescr
$ claude mcp add react-redux-typescript-guide \
-- python -m otcore.mcp_server <graph>