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
@@ -253,6 +254,7 @@ You should also check [the new TypeScript docs for official descriptions between
253
254
# Section 2: Getting Started
254
255
255
256
<!--START-SECTION:function-components-->
257
+
256
258
## Function Components
257
259
258
260
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;
372
374
<!--END-SECTION:function-components-->
373
375
374
376
<!--START-SECTION:hooks-->
377
+
375
378
## Hooks
376
379
377
380
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
650
653
<!--END-SECTION:hooks-->
651
654
652
655
<!--START-SECTION:class-components-->
656
+
653
657
## Class Components
654
658
655
659
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> {
824
828
<!--END-SECTION:class-components-->
825
829
826
830
<!--START-SECTION:default-props-->
831
+
827
832
## You May Not Need `defaultProps`
828
833
829
834
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:
1080
1085
<!--END-SECTION:type-or-interface-->
1081
1086
1082
1087
<!--START-SECTION:basic-type-examples-->
1088
+
1083
1089
## Typing Component Props
1084
1090
1085
1091
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:
1174
1180
1175
1181
- consider using `type` for your React Component Props and State, for consistency and because it is more constrained.
1176
1182
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).
1178
1184
1179
1185
> 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)
1180
1186
@@ -1313,6 +1319,7 @@ class Comp extends React.PureComponent<Props, State> {
1313
1319
<!--END-SECTION:get-derived-state-from-props-->
1314
1320
1315
1321
<!--START-SECTION:forms-and-events-->
1322
+
1316
1323
## Forms and Events
1317
1324
1318
1325
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
_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
1927
1939
<!--END-SECTION:concurrent-->
1928
1940
1929
1941
<!--START-SECTION:types-->
1942
+
1930
1943
# Troubleshooting Handbook: Types
1931
1944
1932
1945
> ⚠️ 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
2470
2483
<!--END-SECTION:types-->
2471
2484
2472
2485
<!--START-SECTION:operators-->
2486
+
2473
2487
# Troubleshooting Handbook: Operators
2474
2488
2475
2489
- `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
2492
2506
<!--END-SECTION:operators-->
2493
2507
2494
2508
<!--START-SECTION:utilities-->
2509
+
2495
2510
# Troubleshooting Handbook: Utilities
2496
2511
2497
2512
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
2513
2528
<!--END-SECTION:utilities-->
2514
2529
2515
2530
<!--START-SECTION:ts-config-->
2531
+
2516
2532
# Troubleshooting Handbook: tsconfig.json
2517
2533
2518
2534
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
2564
2580
<!--END-SECTION:ts-config-->
2565
2581
2566
2582
<!--START-SECTION:official-typings-bugs-->
2583
+
2567
2584
# Troubleshooting Handbook: Fixing bugs in official typings
2568
2585
2569
2586
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
2630
2647
<!--END-SECTION:official-typings-bugs-->
2631
2648
2632
2649
<!--START-SECTION:non-ts-files-->
2650
+
2633
2651
# Time to Really Learn TypeScript
2634
2652
2635
2653
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:
2646
2664
<!--END-SECTION:non-ts-files-->
2647
2665
2648
2666
<!--START-SECTION:resources-->
2667
+
2649
2668
# Other React + TypeScript resources
2650
2669
2651
2670
- me! <https://twitter.com/swyx>
@@ -2668,6 +2687,7 @@ It is worth mentioning some resources to help you get started:
2668
2687
<!--END-SECTION:resources-->
2669
2688
2670
2689
<!--START-SECTION:editor-integration-->
2690
+
2671
2691
# Editor Tooling and Integration
2672
2692
2673
2693
- VSCode
@@ -2692,6 +2712,7 @@ You may also wish to use alternative logos - [jsx-tsx-logos](https://github.com/
2692
2712
<!--END-SECTION:editor-integration-->
2693
2713
2694
2714
<!--START-SECTION:linting-->
2715
+
2695
2716
# Linting
2696
2717
2697
2718
> ⚠️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://
2822
2843
<!--END-SECTION:other-resources-->
2823
2844
2824
2845
<!--START-SECTION:talks-->
2846
+
2825
2847
# Recommended React + TypeScript talks
2826
2848
2827
2849
- [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://
2830
2852
<!--END-SECTION:talks-->
2831
2853
2832
2854
<!--START-SECTION:learn-ts-->
2855
+
2833
2856
# Time to Really Learn TypeScript
2834
2857
2835
2858
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:
2846
2869
<!--END-SECTION:learn-ts-->
2847
2870
2848
2871
<!--START-SECTION:examples-->
2872
+
2849
2873
# Example App
2850
2874
2851
2875
- [Create React App TypeScript Todo Example 2020](https://github.com/laststance/create-react-app-typescript-todo-example-2020)
Copy file name to clipboardExpand all lines: docs/basic/getting-started/basic-type-examples.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,7 @@ Here's a helpful rule of thumb:
95
95
96
96
- consider using `type` for your React Component Props and State, for consistency and because it is more constrained.
97
97
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).
99
99
100
100
> 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)
0 commit comments