Skip to content

Commit 1e31f1f

Browse files
format
1 parent 6818b85 commit 1e31f1f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@
158158
- [Using Partial Types](#using-partial-types)
159159
- [The Types I need weren't exported!](#the-types-i-need-werent-exported)
160160
- [The Types I need don't exist!](#the-types-i-need-dont-exist)
161-
* [Slapping `any` on everything](#slapping-any-on-everything)
162-
* [Autogenerate types](#autogenerate-types)
163-
* [Typing Exported Hooks](#typing-exported-hooks)
164-
* [Typing Exported Components](#typing-exported-components)<!--END-SECTION:types-toc-->
161+
- [Slapping `any` on everything](#slapping-any-on-everything)
162+
- [Autogenerate types](#autogenerate-types)
163+
- [Typing Exported Hooks](#typing-exported-hooks)
164+
- [Typing Exported Components](#typing-exported-components)<!--END-SECTION:types-toc-->
165165
- [Troubleshooting Handbook: Operators](#troubleshooting-handbook-operators)
166166
- [Troubleshooting Handbook: Utilties](#troubleshooting-handbook-utilities)
167167
- [Troubleshooting Handbook: tsconfig.json](#troubleshooting-handbook-tsconfigjson)
@@ -177,6 +177,7 @@
177177
</details>
178178

179179
<!--START-SECTION:setup-->
180+
180181
# Section 1: Setup TypeScript with React
181182

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

255256
<!--START-SECTION:function-components-->
257+
256258
## Function Components
257259

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

374376
<!--START-SECTION:hooks-->
377+
375378
## Hooks
376379

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

652655
<!--START-SECTION:class-components-->
656+
653657
## Class Components
654658

655659
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:
@@ -824,6 +828,7 @@ class Comp extends React.PureComponent<Props, State> {
824828
<!--END-SECTION:class-components-->
825829

826830
<!--START-SECTION:default-props-->
831+
827832
## You May Not Need `defaultProps`
828833

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

10821087
<!--START-SECTION:basic-type-examples-->
1088+
10831089
## Typing Component Props
10841090

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

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

1177-
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).
1183+
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).
11781184

11791185
> 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)
11801186
@@ -1313,6 +1319,7 @@ class Comp extends React.PureComponent<Props, State> {
13131319
<!--END-SECTION:get-derived-state-from-props-->
13141320

13151321
<!--START-SECTION:forms-and-events-->
1322+
13161323
## Forms and Events
13171324

13181325
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):
@@ -1414,6 +1421,7 @@ Of course, if you're making any sort of significant form, [you should use Formik
14141421
<!--END-SECTION:forms-and-events-->
14151422

14161423
<!--START-SECTION:context-->
1424+
14171425
## Context
14181426

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

16981706
<!--START-SECTION:forward-create-ref-->
1707+
16991708
## forwardRef/createRef
17001709

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

17601769
<!--START-SECTION:portals-->
1770+
17611771
## Portals
17621772

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

18661876
<!--START-SECTION:error-boundaries-->
1877+
18671878
## Error Boundaries
18681879

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

19201931
<!--START-SECTION:concurrent-->
1932+
19211933
## Concurrent React/React Suspense
19221934

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

19291941
<!--START-SECTION:types-->
1942+
19301943
# Troubleshooting Handbook: Types
19311944

19321945
> ⚠️ Have you read [the TypeScript FAQ](https://github.com/microsoft/TypeScript/wiki/FAQ?) Your answer might be there!
@@ -2470,6 +2483,7 @@ For more information on creating type definitions for class components, you can
24702483
<!--END-SECTION:types-->
24712484
24722485
<!--START-SECTION:operators-->
2486+
24732487
# Troubleshooting Handbook: Operators
24742488
24752489
- `typeof` and `instanceof`: type query used for refinement
@@ -2492,6 +2506,7 @@ Conditional Types are a difficult topic to get around so here are some extra res
24922506
<!--END-SECTION:operators-->
24932507
24942508
<!--START-SECTION:utilities-->
2509+
24952510
# Troubleshooting Handbook: Utilities
24962511
24972512
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):
@@ -2513,6 +2528,7 @@ These are all built in, [see source in es5.d.ts](https://github.com/microsoft/Ty
25132528
<!--END-SECTION:utilities-->
25142529
25152530
<!--START-SECTION:ts-config-->
2531+
25162532
# Troubleshooting Handbook: tsconfig.json
25172533
25182534
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`):
@@ -2564,6 +2580,7 @@ Compilation speed grows linearly with size of codebase. For large projects, you
25642580
<!--END-SECTION:ts-config-->
25652581
25662582
<!--START-SECTION:official-typings-bugs-->
2583+
25672584
# Troubleshooting Handbook: Fixing bugs in official typings
25682585
25692586
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`:
@@ -2630,6 +2647,7 @@ You can see examples of these included in the built in type declarations in the
26302647
<!--END-SECTION:official-typings-bugs-->
26312648
26322649
<!--START-SECTION:non-ts-files-->
2650+
26332651
# Time to Really Learn TypeScript
26342652
26352653
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 :)
@@ -2646,6 +2664,7 @@ It is worth mentioning some resources to help you get started:
26462664
<!--END-SECTION:non-ts-files-->
26472665
26482666
<!--START-SECTION:resources-->
2667+
26492668
# Other React + TypeScript resources
26502669
26512670
- me! <https://twitter.com/swyx>
@@ -2668,6 +2687,7 @@ It is worth mentioning some resources to help you get started:
26682687
<!--END-SECTION:resources-->
26692688
26702689
<!--START-SECTION:editor-integration-->
2690+
26712691
# Editor Tooling and Integration
26722692
26732693
- VSCode
@@ -2692,6 +2712,7 @@ You may also wish to use alternative logos - [jsx-tsx-logos](https://github.com/
26922712
<!--END-SECTION:editor-integration-->
26932713
26942714
<!--START-SECTION:linting-->
2715+
26952716
# Linting
26962717
26972718
> ⚠️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).
@@ -2822,6 +2843,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
28222843
<!--END-SECTION:other-resources-->
28232844
28242845
<!--START-SECTION:talks-->
2846+
28252847
# Recommended React + TypeScript talks
28262848
28272849
- [Ultimate React Component Patterns with TypeScript](https://www.youtube.com/watch?v=_PBQ3if6Fmg), by Martin Hochel, GeeCon Prague 2018
@@ -2830,6 +2852,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
28302852
<!--END-SECTION:talks-->
28312853
28322854
<!--START-SECTION:learn-ts-->
2855+
28332856
# Time to Really Learn TypeScript
28342857
28352858
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 :)
@@ -2846,6 +2869,7 @@ It is worth mentioning some resources to help you get started:
28462869
<!--END-SECTION:learn-ts-->
28472870
28482871
<!--START-SECTION:examples-->
2872+
28492873
# Example App
28502874
28512875
- [Create React App TypeScript Todo Example 2020](https://github.com/laststance/create-react-app-typescript-todo-example-2020)

docs/advanced/patterns_by_version.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ let stuff = h(Fragment, null, h("div", null, "Hello"));
451451

452452
1. Template Literal Types
453453

454-
This is a HUGE feature.
454+
This is a HUGE feature.
455455

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

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

514-
515514
Misc
516515

517516
2. [Key Remapping in Mapped Types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#key-remapping-in-mapped-types)

docs/basic/getting-started/basic-type-examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Here's a helpful rule of thumb:
9595

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

98-
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).
98+
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).
9999

100100
> 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)
101101

0 commit comments

Comments
 (0)