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.
@@ -375,6 +377,7 @@ const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
375
377
<!--END-SECTION:function-components-->
376
378
377
379
<!--START-SECTION:hooks-->
380
+
378
381
## Hooks
379
382
380
383
Hooks are [supported in `@types/react` from v16.8 up](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a05cc538a42243c632f054e42eab483ebf1560ab/types/react/index.d.ts#L800-L1031).
@@ -653,6 +656,7 @@ If you are writing a React Hooks library, don't forget that you should also expo
653
656
<!--END-SECTION:hooks-->
654
657
655
658
<!--START-SECTION:class-components-->
659
+
656
660
## Class Components
657
661
658
662
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:
@@ -827,6 +831,7 @@ class Comp extends React.PureComponent<Props, State> {
827
831
<!--END-SECTION:class-components-->
828
832
829
833
<!--START-SECTION:default-props-->
834
+
830
835
## You May Not Need `defaultProps`
831
836
832
837
As per [this tweet](https://twitter.com/dan_abramov/status/1133878326358171650), defaultProps will eventually be deprecated. You can check the discussions here:
@@ -1037,6 +1042,7 @@ The problem with this approach is it causes complex issues with the type inferen
1037
1042
<!--END-SECTION:default-props-->
1038
1043
1039
1044
<!--START-SECTION:basic-type-examples-->
1045
+
1040
1046
## Typing Component Props
1041
1047
1042
1048
This is intended as a basic orientation and reference for React developers familiarizing with TypeScript.
@@ -1260,6 +1266,7 @@ class Comp extends React.PureComponent<Props, State> {
1260
1266
<!--END-SECTION:get-derived-state-from-props-->
1261
1267
1262
1268
<!--START-SECTION:forms-and-events-->
1269
+
1263
1270
## Forms and Events
1264
1271
1265
1272
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):
@@ -1361,6 +1368,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.
@@ -1877,6 +1889,7 @@ _Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more o
1877
1889
<!--END-SECTION:concurrent-->
1878
1890
1879
1891
<!--START-SECTION:types-->
1892
+
1880
1893
# Troubleshooting Handbook: Types
1881
1894
1882
1895
> ⚠️ Have you read [the TypeScript FAQ](https://github.com/microsoft/TypeScript/wiki/FAQ?) Your answer might be there!
@@ -2417,6 +2430,7 @@ For more information on creating type definitions for class components, you can
2417
2430
<!--END-SECTION:types-->
2418
2431
2419
2432
<!--START-SECTION:operators-->
2433
+
2420
2434
# Troubleshooting Handbook: Operators
2421
2435
2422
2436
- `typeof` and `instanceof`: type query used for refinement
@@ -2440,6 +2454,7 @@ Conditional Types are a difficult topic to get around so here are some extra res
2440
2454
<!--END-SECTION:operators-->
2441
2455
2442
2456
<!--START-SECTION:utilities-->
2457
+
2443
2458
# Troubleshooting Handbook: Utilities
2444
2459
2445
2460
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):
@@ -2461,6 +2476,7 @@ These are all built in, [see source in es5.d.ts](https://github.com/microsoft/Ty
2461
2476
<!--END-SECTION:utilities-->
2462
2477
2463
2478
<!--START-SECTION:ts-config-->
2479
+
2464
2480
# Troubleshooting Handbook: tsconfig.json
2465
2481
2466
2482
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`):
@@ -2512,6 +2528,7 @@ Compilation speed grows linearly with size of codebase. For large projects, you
2512
2528
<!--END-SECTION:ts-config-->
2513
2529
2514
2530
<!--START-SECTION:official-typings-bugs-->
2531
+
2515
2532
# Troubleshooting Handbook: Fixing bugs in official typings
2516
2533
2517
2534
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`:
@@ -2578,6 +2595,7 @@ You can see examples of these included in the built in type declarations in the
2578
2595
<!--END-SECTION:official-typings-bugs-->
2579
2596
2580
2597
<!--START-SECTION:non-ts-files-->
2598
+
2581
2599
# Time to Really Learn TypeScript
2582
2600
2583
2601
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.
@@ -2598,6 +2616,7 @@ It is worth mentioning some resources to help you get started:
2598
2616
<!--END-SECTION:non-ts-files-->
2599
2617
2600
2618
<!--START-SECTION:resources-->
2619
+
2601
2620
# Other React + TypeScript resources
2602
2621
2603
2622
- me! <https://twitter.com/swyx>
@@ -2620,6 +2639,7 @@ It is worth mentioning some resources to help you get started:
2620
2639
<!--END-SECTION:resources-->
2621
2640
2622
2641
<!--START-SECTION:editor-integration-->
2642
+
2623
2643
# Editor Tooling and Integration
2624
2644
2625
2645
- VSCode
@@ -2644,6 +2664,7 @@ You may also wish to use alternative logos - [jsx-tsx-logos](https://github.com/
2644
2664
<!--END-SECTION:editor-integration-->
2645
2665
2646
2666
<!--START-SECTION:linting-->
2667
+
2647
2668
# Linting
2648
2669
2649
2670
> ⚠️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).
@@ -2774,6 +2795,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
2774
2795
<!--END-SECTION:other-resources-->
2775
2796
2776
2797
<!--START-SECTION:talks-->
2798
+
2777
2799
# Recommended React + TypeScript talks
2778
2800
2779
2801
- [Ultimate React Component Patterns with TypeScript](https://www.youtube.com/watch?v=_PBQ3if6Fmg), by Martin Hochel, GeeCon Prague 2018
@@ -2782,6 +2804,7 @@ If you're looking for information on Prettier, check out the [Prettier](https://
2782
2804
<!--END-SECTION:talks-->
2783
2805
2784
2806
<!--START-SECTION:learn-ts-->
2807
+
2785
2808
# Time to Really Learn TypeScript
2786
2809
2787
2810
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.
@@ -2802,6 +2825,7 @@ It is worth mentioning some resources to help you get started:
2802
2825
<!--END-SECTION:learn-ts-->
2803
2826
2804
2827
<!--START-SECTION:examples-->
2828
+
2805
2829
# Example App
2806
2830
2807
2831
- [Create React App TypeScript Todo Example 2020](https://github.com/laststance/create-react-app-typescript-todo-example-2020)
0 commit comments