You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -254,6 +255,7 @@ You should also check [the new TypeScript docs for official descriptions between
254
255
# Section 2: Getting Started
255
256
256
257
<!--START-SECTION:function-components-->
258
+
257
259
## Function Components
258
260
259
261
These can be written as normal functions that take a `props` argument and return a JSX element.
@@ -382,6 +384,7 @@ const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
382
384
<!--END-SECTION:function-components-->
383
385
384
386
<!--START-SECTION:hooks-->
387
+
385
388
## Hooks
386
389
387
390
Hooks are [supported in `@types/react` from v16.8 up](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a05cc538a42243c632f054e42eab483ebf1560ab/types/react/index.d.ts#L800-L1031).
@@ -665,6 +668,7 @@ If you are writing a React Hooks library, don't forget that you should also expo
665
668
<!--END-SECTION:hooks-->
666
669
667
670
<!--START-SECTION:class-components-->
671
+
668
672
## Class Components
669
673
670
674
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:
@@ -839,6 +843,7 @@ class Comp extends React.PureComponent<Props, State> {
839
843
<!--END-SECTION:class-components-->
840
844
841
845
<!--START-SECTION:default-props-->
846
+
842
847
## You May Not Need `defaultProps`
843
848
844
849
As per [this tweet](https://twitter.com/dan_abramov/status/1133878326358171650), defaultProps will eventually be deprecated. You can check the discussions here:
@@ -1049,6 +1054,7 @@ The problem with this approach is it causes complex issues with the type inferen
1049
1054
<!--END-SECTION:default-props-->
1050
1055
1051
1056
<!--START-SECTION:basic-type-examples-->
1057
+
1052
1058
## Typing Component Props
1053
1059
1054
1060
This is intended as a basic orientation and reference for React developers familiarizing with TypeScript.
@@ -1280,6 +1286,7 @@ class Comp extends React.PureComponent<Props, State> {
1280
1286
<!--END-SECTION:get-derived-state-from-props-->
1281
1287
1282
1288
<!--START-SECTION:forms-and-events-->
1289
+
1283
1290
## Forms and Events
1284
1291
1285
1292
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):
@@ -1381,6 +1388,7 @@ Of course, if you're making any sort of significant form, [you should use Formik
_Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more on React Suspense and Time Slicing.
@@ -1897,6 +1909,7 @@ _Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more o
1897
1909
<!--END-SECTION:concurrent-->
1898
1910
1899
1911
<!--START-SECTION:types-->
1912
+
1900
1913
# Troubleshooting Handbook: Types
1901
1914
1902
1915
> ⚠️ Have you read [the TypeScript FAQ](https://github.com/microsoft/TypeScript/wiki/FAQ?) Your answer might be there!
@@ -2442,6 +2455,7 @@ For more information on creating type definitions for class components, you can
2442
2455
<!--END-SECTION:types-->
2443
2456
2444
2457
<!--START-SECTION:operators-->
2458
+
2445
2459
# Troubleshooting Handbook: Operators
2446
2460
2447
2461
- `typeof` and `instanceof`: type query used for refinement
@@ -2465,6 +2479,7 @@ Conditional Types are a difficult topic to get around so here are some extra res
2465
2479
<!--END-SECTION:operators-->
2466
2480
2467
2481
<!--START-SECTION:utilities-->
2482
+
2468
2483
# Troubleshooting Handbook: Utilities
2469
2484
2470
2485
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):
@@ -2486,6 +2501,7 @@ These are all built in, [see source in es5.d.ts](https://github.com/microsoft/Ty
2486
2501
<!--END-SECTION:utilities-->
2487
2502
2488
2503
<!--START-SECTION:ts-config-->
2504
+
2489
2505
# Troubleshooting Handbook: tsconfig.json
2490
2506
2491
2507
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`):
@@ -2537,6 +2553,7 @@ Compilation speed grows linearly with size of codebase. For large projects, you
2537
2553
<!--END-SECTION:ts-config-->
2538
2554
2539
2555
<!--START-SECTION:official-typings-bugs-->
2556
+
2540
2557
# Troubleshooting Handbook: Fixing bugs in official typings
2541
2558
2542
2559
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`:
@@ -2603,6 +2620,7 @@ You can see examples of these included in the built in type declarations in the
2603
2620
<!--END-SECTION:official-typings-bugs-->
2604
2621
2605
2622
<!--START-SECTION:non-ts-files-->
2623
+
2606
2624
# Time to Really Learn TypeScript
2607
2625
2608
2626
Believe it or not, we have only barely introduced TypeScript here in this cheatsheet. If you are still facing TypeScript troubleshooting issues, it is likely that your understanding of TS is still too superficial.
@@ -2624,6 +2642,7 @@ It is worth mentioning some resources to help you get started:
2624
2642
<!--END-SECTION:non-ts-files-->
2625
2643
2626
2644
<!--START-SECTION:resources-->
2645
+
2627
2646
# Other React + TypeScript resources
2628
2647
2629
2648
- me! <https://twitter.com/swyx>
@@ -2657,6 +2676,7 @@ It is worth mentioning some resources to help you get started:
2657
2676
<!--END-SECTION:resources-->
2658
2677
2659
2678
<!--START-SECTION:editor-integration-->
2679
+
2660
2680
# Editor Tooling and Integration
2661
2681
2662
2682
- VSCode
@@ -2681,6 +2701,7 @@ You may also wish to use alternative logos - [jsx-tsx-logos](https://github.com/
2681
2701
<!--END-SECTION:editor-integration-->
2682
2702
2683
2703
<!--START-SECTION:linting-->
2704
+
2684
2705
# Linting
2685
2706
2686
2707
> ⚠️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).
@@ -2811,6 +2832,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
2811
2832
<!--END-SECTION:other-resources-->
2812
2833
2813
2834
<!--START-SECTION:talks-->
2835
+
2814
2836
# Recommended React + TypeScript talks
2815
2837
2816
2838
- [Ultimate React Component Patterns with TypeScript](https://www.youtube.com/watch?v=_PBQ3if6Fmg), by Martin Hochel, GeeCon Prague 2018
@@ -2819,6 +2841,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
2819
2841
<!--END-SECTION:talks-->
2820
2842
2821
2843
<!--START-SECTION:learn-ts-->
2844
+
2822
2845
# Time to Really Learn TypeScript
2823
2846
2824
2847
Believe it or not, we have only barely introduced TypeScript here in this cheatsheet. If you are still facing TypeScript troubleshooting issues, it is likely that your understanding of TS is still too superficial.
@@ -2840,6 +2863,7 @@ It is worth mentioning some resources to help you get started:
2840
2863
<!--END-SECTION:learn-ts-->
2841
2864
2842
2865
<!--START-SECTION:examples-->
2866
+
2843
2867
# Example App
2844
2868
2845
2869
- [Create React App TypeScript Todo Example 2020](https://github.com/laststance/create-react-app-typescript-todo-example-2020)
0 commit comments