Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman-Sherman committed Dec 8, 2020
1 parent eabfe3a commit 0b6ba25
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@
- [Using Partial Types](#using-partial-types)
- [The Types I need weren't exported!](#the-types-i-need-werent-exported)
- [The Types I need don't exist!](#the-types-i-need-dont-exist)
* [Slapping `any` on everything](#slapping-any-on-everything)
* [Autogenerate types](#autogenerate-types)
* [Typing Exported Hooks](#typing-exported-hooks)
* [Typing Exported Components](#typing-exported-components)<!--END-SECTION:types-toc-->
- [Slapping `any` on everything](#slapping-any-on-everything)
- [Autogenerate types](#autogenerate-types)
- [Typing Exported Hooks](#typing-exported-hooks)
- [Typing Exported Components](#typing-exported-components)<!--END-SECTION:types-toc-->
- [Troubleshooting Handbook: Operators](#troubleshooting-handbook-operators)
- [Troubleshooting Handbook: Utilties](#troubleshooting-handbook-utilities)
- [Troubleshooting Handbook: tsconfig.json](#troubleshooting-handbook-tsconfigjson)
Expand All @@ -177,6 +177,7 @@
</details>

<!--START-SECTION:setup-->

# Section 1: Setup TypeScript with React

## Prerequisites
Expand Down Expand Up @@ -253,6 +254,7 @@ You should also check [the new TypeScript docs for official descriptions between
# Section 2: Getting Started

<!--START-SECTION:function-components-->

## Function Components

These can be written as normal functions that take a `props` argument and return a JSX element.
Expand Down Expand Up @@ -372,6 +374,7 @@ const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
<!--END-SECTION:function-components-->

<!--START-SECTION:hooks-->

## Hooks

Hooks are [supported in `@types/react` from v16.8 up](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a05cc538a42243c632f054e42eab483ebf1560ab/types/react/index.d.ts#L800-L1031).
Expand Down Expand Up @@ -650,6 +653,7 @@ If you are writing a React Hooks library, don't forget that you should also expo
<!--END-SECTION:hooks-->

<!--START-SECTION:class-components-->

## Class Components

Within TypeScript, `React.Component` is a generic type (aka `React.Component<PropType, StateType>`), so you want to provide it with (optional) prop and state type parameters:
Expand Down Expand Up @@ -824,6 +828,7 @@ class Comp extends React.PureComponent<Props, State> {
<!--END-SECTION:class-components-->

<!--START-SECTION:default-props-->

## You May Not Need `defaultProps`

As per [this tweet](https://twitter.com/dan_abramov/status/1133878326358171650), defaultProps will eventually be deprecated. You can check the discussions here:
Expand Down Expand Up @@ -1080,6 +1085,7 @@ It's a nuanced topic, don't get too hung up on it. Here's a handy table:
<!--END-SECTION:type-or-interface-->

<!--START-SECTION:basic-type-examples-->

## Typing Component Props

This is intended as a basic orientation and reference for React developers familiarizing with TypeScript.
Expand Down Expand Up @@ -1174,7 +1180,7 @@ Here's a helpful rule of thumb:

- consider using `type` for your React Component Props and State, for consistency and because it is more constrained.

You can read more about the reasoning behind this rule of thumb in [Interface vs Type alias in TypeScript 2.7](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c).
You can read more about the reasoning behind this rule of thumb in [Interface vs Type alias in TypeScript 2.7](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c).

> Note: At scale, there are performance reasons to prefer interfaces ([see official Microsoft notes on this](https://github.com/microsoft/TypeScript/wiki/Performance#preferring-interfaces-over-intersections)) but [take this with a grain of salt](https://news.ycombinator.com/item?id=25201887)
Expand Down Expand Up @@ -1313,6 +1319,7 @@ class Comp extends React.PureComponent<Props, State> {
<!--END-SECTION:get-derived-state-from-props-->

<!--START-SECTION:forms-and-events-->

## Forms and Events

If performance is not an issue (and it usually isn't!), inlining handlers is easiest as you can just use [type inference and contextual typing](https://www.typescriptlang.org/docs/handbook/type-inference.html#contextual-typing):
Expand Down Expand Up @@ -1414,6 +1421,7 @@ Of course, if you're making any sort of significant form, [you should use Formik
<!--END-SECTION:forms-and-events-->

<!--START-SECTION:context-->

## Context

## Basic Example
Expand Down Expand Up @@ -1696,6 +1704,7 @@ const Consumer = Context.Consumer;
<!--END-SECTION:context-->

<!--START-SECTION:forward-create-ref-->

## forwardRef/createRef

Check the [Hooks section](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/blob/master/README.md#hooks) for `useRef`.
Expand Down Expand Up @@ -1758,6 +1767,7 @@ You may also wish to do [Conditional Rendering with `forwardRef`](https://github
<!--END-SECTION:forward-create-ref-->

<!--START-SECTION:portals-->

## Portals

Using `ReactDOM.createPortal`:
Expand Down Expand Up @@ -1864,6 +1874,7 @@ This example is based on the [Event Bubbling Through Portal](https://reactjs.org
<!--END-SECTION:portals-->

<!--START-SECTION:error-boundaries-->

## Error Boundaries

### Option 1: Using react-error-boundary
Expand Down Expand Up @@ -1918,6 +1929,7 @@ export default ErrorBoundary;
<!--END-SECTION:error-boundaries-->

<!--START-SECTION:concurrent-->

## Concurrent React/React Suspense

_Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more on React Suspense and Time Slicing.
Expand All @@ -1927,6 +1939,7 @@ _Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more o
<!--END-SECTION:concurrent-->

<!--START-SECTION:types-->

# Troubleshooting Handbook: Types

> ⚠️ Have you read [the TypeScript FAQ](https://github.com/microsoft/TypeScript/wiki/FAQ?) Your answer might be there!
Expand Down Expand Up @@ -2470,6 +2483,7 @@ For more information on creating type definitions for class components, you can
<!--END-SECTION:types-->
<!--START-SECTION:operators-->
# Troubleshooting Handbook: Operators
- `typeof` and `instanceof`: type query used for refinement
Expand All @@ -2492,6 +2506,7 @@ Conditional Types are a difficult topic to get around so here are some extra res
<!--END-SECTION:operators-->
<!--START-SECTION:utilities-->
# Troubleshooting Handbook: Utilities
These are all built in, [see source in es5.d.ts](https://github.com/microsoft/TypeScript/blob/2c458c0d1ccb96442bca9ce43aa987fb0becf8a9/src/lib/es5.d.ts#L1401-L1474):
Expand All @@ -2513,6 +2528,7 @@ These are all built in, [see source in es5.d.ts](https://github.com/microsoft/Ty
<!--END-SECTION:utilities-->
<!--START-SECTION:ts-config-->
# Troubleshooting Handbook: tsconfig.json
You can find [all the Compiler options in the TypeScript docs](https://www.typescriptlang.org/docs/handbook/compiler-options.html). [The new TS docs also has per-flag annotations of what each does](https://www.typescriptlang.org/tsconfig#allowSyntheticDefaultImports). This is the setup I roll with for APPS (not libraries - for libraries you may wish to see the settings we use in `tsdx`):
Expand Down Expand Up @@ -2564,6 +2580,7 @@ Compilation speed grows linearly with size of codebase. For large projects, you
<!--END-SECTION:ts-config-->
<!--START-SECTION:official-typings-bugs-->
# Troubleshooting Handbook: Fixing bugs in official typings
If you run into bugs with your library's official typings, you can copy them locally and tell TypeScript to use your local version using the "paths" field. In your `tsconfig.json`:
Expand Down Expand Up @@ -2630,6 +2647,7 @@ You can see examples of these included in the built in type declarations in the
<!--END-SECTION:official-typings-bugs-->
<!--START-SECTION:non-ts-files-->
# Time to Really Learn TypeScript
Believe it or not, we have only barely introduced TypeScript here in this cheatsheet. There is a whole world of generic type logic that you will eventually get into, however it becomes far less dealing with React than just getting good at TypeScript so it is out of scope here. But at least you can get productive in React now :)
Expand All @@ -2646,6 +2664,7 @@ It is worth mentioning some resources to help you get started:
<!--END-SECTION:non-ts-files-->
<!--START-SECTION:resources-->
# Other React + TypeScript resources
- me! <https://twitter.com/swyx>
Expand All @@ -2668,6 +2687,7 @@ It is worth mentioning some resources to help you get started:
<!--END-SECTION:resources-->
<!--START-SECTION:editor-integration-->
# Editor Tooling and Integration
- VSCode
Expand All @@ -2692,6 +2712,7 @@ You may also wish to use alternative logos - [jsx-tsx-logos](https://github.com/
<!--END-SECTION:editor-integration-->
<!--START-SECTION:linting-->
# Linting
> ⚠️Note that [TSLint is now in maintenance and you should try to use ESLint instead](https://medium.com/palantir/tslint-in-2019-1a144c2317a9). If you are interested in TSLint tips, please check this PR from [@azdanov](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/pull/14). The rest of this section just focuses on ESLint. [You can convert TSlint to ESlint with this tool](https://github.com/typescript-eslint/tslint-to-eslint-config).
Expand Down Expand Up @@ -2822,6 +2843,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
<!--END-SECTION:other-resources-->
<!--START-SECTION:talks-->
# Recommended React + TypeScript talks
- [Ultimate React Component Patterns with TypeScript](https://www.youtube.com/watch?v=_PBQ3if6Fmg), by Martin Hochel, GeeCon Prague 2018
Expand All @@ -2830,6 +2852,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
<!--END-SECTION:talks-->
<!--START-SECTION:learn-ts-->
# Time to Really Learn TypeScript
Believe it or not, we have only barely introduced TypeScript here in this cheatsheet. There is a whole world of generic type logic that you will eventually get into, however it becomes far less dealing with React than just getting good at TypeScript so it is out of scope here. But at least you can get productive in React now :)
Expand All @@ -2846,6 +2869,7 @@ It is worth mentioning some resources to help you get started:
<!--END-SECTION:learn-ts-->
<!--START-SECTION:examples-->
# Example App
- [Create React App TypeScript Todo Example 2020](https://github.com/laststance/create-react-app-typescript-todo-example-2020)
Expand Down
3 changes: 1 addition & 2 deletions docs/advanced/patterns_by_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ let stuff = h(Fragment, null, h("div", null, "Hello"));

1. Template Literal Types

This is a HUGE feature.
This is a HUGE feature.

Usecase 1 - Generating string literal types from permutations of other string literal types:

Expand Down Expand Up @@ -511,7 +511,6 @@ This is a new compiler option to offer output inline with React 17 support in ge
}
```


Misc

2. [Key Remapping in Mapped Types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#key-remapping-in-mapped-types)
Expand Down
2 changes: 1 addition & 1 deletion docs/basic/getting-started/basic-type-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Here's a helpful rule of thumb:

- consider using `type` for your React Component Props and State, for consistency and because it is more constrained.

You can read more about the reasoning behind this rule of thumb in [Interface vs Type alias in TypeScript 2.7](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c).
You can read more about the reasoning behind this rule of thumb in [Interface vs Type alias in TypeScript 2.7](https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c).

> Note: At scale, there are performance reasons to prefer interfaces ([see official Microsoft notes on this](https://github.com/microsoft/TypeScript/wiki/Performance#preferring-interfaces-over-intersections)) but [take this with a grain of salt](https://news.ycombinator.com/item?id=25201887)
Expand Down

0 comments on commit 0b6ba25

Please sign in to comment.