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
@@ -255,7 +254,6 @@ You should also check [the new TypeScript docs for official descriptions between
255
254
# Section 2: Getting Started
256
255
257
256
<!--START-SECTION:function-components-->
258
-
259
257
## Function Components
260
258
261
259
These can be written as normal functions that take a `props` argument and return a JSX element.
@@ -384,14 +382,13 @@ const MyArrayComponent = () => (Array(5).fill(<div />) as any) as JSX.Element;
384
382
<!--END-SECTION:function-components-->
385
383
386
384
<!--START-SECTION:hooks-->
387
-
388
385
## Hooks
389
386
390
387
Hooks are [supported in `@types/react` from v16.8 up](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a05cc538a42243c632f054e42eab483ebf1560ab/types/react/index.d.ts#L800-L1031).
This temporarily "lies" to the TypeScript compiler that `{}` is of type `IUser`. You should follow up by setting the `user` state — if you don't, the rest of your code may rely on the fact that `user` is of type `IUser` and that may lead to runtime errors.
420
+
413
421
## useReducer
414
422
415
423
You can use [Discriminated Unions](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions) for reducer actions. Don't forget to define the return type of reducer, otherwise TypeScript will infer it.
@@ -510,7 +518,7 @@ function DelayedEffect(props: { timerMs: number }) {
510
518
511
519
## useRef
512
520
513
-
When using `useRef`, you have three options when creating a ref container that does not have an initial value:
521
+
When using `useRef`, you have two options when creating a ref container that does not have an initial value:
514
522
515
523
```ts
516
524
const ref1 =useRef<HTMLElement>(null!);
@@ -668,7 +676,6 @@ If you are writing a React Hooks library, don't forget that you should also expo
668
676
<!--END-SECTION:hooks-->
669
677
670
678
<!--START-SECTION:class-components-->
671
-
672
679
## Class Components
673
680
674
681
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:
@@ -843,7 +850,6 @@ class Comp extends React.PureComponent<Props, State> {
843
850
<!--END-SECTION:class-components-->
844
851
845
852
<!--START-SECTION:default-props-->
846
-
847
853
## You May Not Need `defaultProps`
848
854
849
855
As per [this tweet](https://twitter.com/dan_abramov/status/1133878326358171650), defaultProps will eventually be deprecated. You can check the discussions here:
@@ -1054,7 +1060,6 @@ The problem with this approach is it causes complex issues with the type inferen
1054
1060
<!--END-SECTION:default-props-->
1055
1061
1056
1062
<!--START-SECTION:basic-type-examples-->
1057
-
1058
1063
## Typing Component Props
1059
1064
1060
1065
This is intended as a basic orientation and reference for React developers familiarizing with TypeScript.
@@ -1286,7 +1291,6 @@ class Comp extends React.PureComponent<Props, State> {
1286
1291
<!--END-SECTION:get-derived-state-from-props-->
1287
1292
1288
1293
<!--START-SECTION:forms-and-events-->
1289
-
1290
1294
## Forms and Events
1291
1295
1292
1296
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):
@@ -1347,7 +1351,7 @@ The first method uses an inferred method signature `(e: React.FormEvent<HTMLInpu
1347
1351
1348
1352
**Typing onSubmit, with Uncontrolled components in a Form**
1349
1353
1350
-
If you don't quite care about the type of the event, you can just use React.SyntheticEvent. If your target form has custom named inputs that you'd like to access, you can use type widening:
1354
+
If you don't quite care about the type of the event, you can just use React.SyntheticEvent. If your target form has custom named inputs that you'd like to access, you can use a type assertion:
1351
1355
1352
1356
```tsx
1353
1357
<form
@@ -1385,10 +1389,47 @@ If you don't quite care about the type of the event, you can just use React.Synt
1385
1389
1386
1390
Of course, if you're making any sort of significant form, [you should use Formik](https://jaredpalmer.com/formik) or [React Hook Form](https://react-hook-form.com/), which are written in TypeScript.
| ChangeEvent | Changing the value of `<input>`, `<select>` and `<textarea>` element. |
1398
+
| ClipboardEvent | Using copy, paste and cut events. |
1399
+
| CompositionEvent | Events that occur due to the user indirectly entering text (e.g. depending on Browser and PC setup, a popup window may appear with additional characters if you e.g. want to type Japanese on a US Keyboard) |
1400
+
| DragEvent | Drag and drop interaction with a pointer device (e.g. mouse). |
1401
+
| FocusEvent | Event that occurs when elements gets or loses focus. |
1402
+
| FormEvent | Event that occurs whenever a form or form element gets/loses focus, a form element value is changed or the form is submitted. |
1403
+
| InvalidEvent | Fired when validity restrictions of an input fails (e.g `<input type="number" max="10">` and someone would insert number 20). |
1404
+
| KeyboardEvent | User interaction with the keyboard. Each event describes a single key interaction. |
1405
+
| MouseEvent | Events that occur due to the user interacting with a pointing device (e.g. mouse) |
1406
+
| PointerEvent | Events that occur due to user interaction with a variety pointing of devices such as mouse, pen/stylus, a touchscreen and which also supports multi-touch. Unless you develop for older browsers (IE10 or Safari 12), pointer events are recommended. Extends UIEvent. |
1407
+
| TouchEvent | Events that occur due to the user interacting with a touch device. Extends UIEvent. |
| UIEvent | Base Event for Mouse, Touch and Pointer events. |
1410
+
| WheelEvent | Scrolling on a mouse wheel or similar input device. (Note: `wheel` event should not be confused with the `scroll` event) |
1411
+
| SyntheticEvent | The base event for all above events. Should be used when unsure about event type |
1412
+
1413
+
<details>
1414
+
<summary>
1415
+
1416
+
**What about `InputEvent`?**
1417
+
1418
+
</summary>
1419
+
1420
+
You've probably noticed that there is no `InputEvent`. This is because it is not supported by Typescript as the event itself has no fully browser support and may behave differently in different browsers. You can use `KeyboardEvent` instead.
_Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more on React Suspense and Time Slicing.
@@ -1909,7 +1946,6 @@ _Not written yet._ watch <https://github.com/sw-yx/fresh-async-react> for more o
1909
1946
<!--END-SECTION:concurrent-->
1910
1947
1911
1948
<!--START-SECTION:types-->
1912
-
1913
1949
# Troubleshooting Handbook: Types
1914
1950
1915
1951
> ⚠️ Have you read [the TypeScript FAQ](https://github.com/microsoft/TypeScript/wiki/FAQ?) Your answer might be there!
@@ -2045,9 +2081,9 @@ Note that you cannot assert your way to anything - basically it is only for refi
2045
2081
You can also assert a property is non-null, when accessing it:
2046
2082
2047
2083
```ts
2048
-
element.parentNode!.removeChild(element) // ! before the period
2049
-
myFunction(document.getElementById(dialog.id!)!// ! after the property accessing
2050
-
letuserID!: string// definite assignment assertion... be careful!
2084
+
element.parentNode!.removeChild(element);// ! before the period
2085
+
myFunction(document.getElementById(dialog.id!)!);// ! after the property accessing
2086
+
let userID!:string;// definite assignment assertion... be careful!
2051
2087
```
2052
2088
2053
2089
Of course, try to actually handle the null case instead of asserting :)
@@ -2455,7 +2491,6 @@ For more information on creating type definitions for class components, you can
2455
2491
<!--END-SECTION:types-->
2456
2492
2457
2493
<!--START-SECTION:operators-->
2458
-
2459
2494
# Troubleshooting Handbook: Operators
2460
2495
2461
2496
- `typeof` and `instanceof`: type query used for refinement
@@ -2479,7 +2514,6 @@ Conditional Types are a difficult topic to get around so here are some extra res
2479
2514
<!--END-SECTION:operators-->
2480
2515
2481
2516
<!--START-SECTION:utilities-->
2482
-
2483
2517
# Troubleshooting Handbook: Utilities
2484
2518
2485
2519
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):
@@ -2501,7 +2535,6 @@ These are all built in, [see source in es5.d.ts](https://github.com/microsoft/Ty
2501
2535
<!--END-SECTION:utilities-->
2502
2536
2503
2537
<!--START-SECTION:ts-config-->
2504
-
2505
2538
# Troubleshooting Handbook: tsconfig.json
2506
2539
2507
2540
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`):
@@ -2553,7 +2586,6 @@ Compilation speed grows linearly with size of codebase. For large projects, you
2553
2586
<!--END-SECTION:ts-config-->
2554
2587
2555
2588
<!--START-SECTION:official-typings-bugs-->
2556
-
2557
2589
# Troubleshooting Handbook: Fixing bugs in official typings
2558
2590
2559
2591
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`:
@@ -2620,7 +2652,6 @@ You can see examples of these included in the built in type declarations in the
2620
2652
<!--END-SECTION:official-typings-bugs-->
2621
2653
2622
2654
<!--START-SECTION:non-ts-files-->
2623
-
2624
2655
# Time to Really Learn TypeScript
2625
2656
2626
2657
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.
@@ -2642,7 +2673,6 @@ It is worth mentioning some resources to help you get started:
2642
2673
<!--END-SECTION:non-ts-files-->
2643
2674
2644
2675
<!--START-SECTION:resources-->
2645
-
2646
2676
# Other React + TypeScript resources
2647
2677
2648
2678
- me! <https://twitter.com/swyx>
@@ -2676,7 +2706,6 @@ It is worth mentioning some resources to help you get started:
2676
2706
<!--END-SECTION:resources-->
2677
2707
2678
2708
<!--START-SECTION:editor-integration-->
2679
-
2680
2709
# Editor Tooling and Integration
2681
2710
2682
2711
- VSCode
@@ -2701,7 +2730,6 @@ You may also wish to use alternative logos - [jsx-tsx-logos](https://github.com/
2701
2730
<!--END-SECTION:editor-integration-->
2702
2731
2703
2732
<!--START-SECTION:linting-->
2704
-
2705
2733
# Linting
2706
2734
2707
2735
> ⚠️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).
@@ -2832,7 +2860,6 @@ If you're looking for information on Prettier, check out the [Prettier](https://
2832
2860
<!--END-SECTION:other-resources-->
2833
2861
2834
2862
<!--START-SECTION:talks-->
2835
-
2836
2863
# Recommended React + TypeScript talks
2837
2864
2838
2865
- [Ultimate React Component Patterns with TypeScript](https://www.youtube.com/watch?v=_PBQ3if6Fmg), by Martin Hochel, GeeCon Prague 2018
@@ -2841,7 +2868,6 @@ If you're looking for information on Prettier, check out the [Prettier](https://
2841
2868
<!--END-SECTION:talks-->
2842
2869
2843
2870
<!--START-SECTION:learn-ts-->
2844
-
2845
2871
# Time to Really Learn TypeScript
2846
2872
2847
2873
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.
@@ -2863,7 +2889,6 @@ It is worth mentioning some resources to help you get started:
2863
2889
<!--END-SECTION:learn-ts-->
2864
2890
2865
2891
<!--START-SECTION:examples-->
2866
-
2867
2892
# Example App
2868
2893
2869
2894
- [Create React App TypeScript Todo Example 2020](https://github.com/laststance/create-react-app-typescript-todo-example-2020)
0 commit comments