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
@@ -181,6 +184,11 @@ class App extends React.Component<{
181
184
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
182
185
</details>
183
186
187
+
# Typing DefaultProps
188
+
189
+
It is easy to [type defaults for functional components](https://twitter.com/GeeWengel/status/1000242363376205825), but there is some debate in the community on how to type the `static defaultProps` field for class-based components. We have an active discussion on several approaches on how to do this. [Please check out our issue here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/4).
190
+
191
+
184
192
# Extracting Prop Types
185
193
186
194
Instead of defining prop types inline, you can declare them separately (useful for reusability or code organization):
@@ -220,7 +228,6 @@ class App extends React.Component<AppProps, AppState> {
220
228
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
221
229
</details>
222
230
223
-
224
231
# Basic Prop Types Examples
225
232
226
233
```tsx
@@ -393,8 +400,7 @@ This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/r
393
400
394
401
# Working with Non-Typescript Libraries (writing your own index.d.ts)
395
402
396
-
397
-
Please contribute on this topic! [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
403
+
Please contribute on this topic! [We have an ongoing issue here with some references](https://github.com/sw-yx/react-typescript-cheatsheet/issues/8).
@@ -504,10 +510,9 @@ This handy because Typescript will throw errors when you mistype a string for yo
504
510
505
511
</details>
506
512
507
-
## Type Casting
508
-
509
-
Sometimes union types need to be cast to a more specific type to work with other APIs, so cast with the `as` keyword.
513
+
## Type Assertion
510
514
515
+
Sometimes Typescript is just getting your type wrong, or union types need to be asserted to a more specific type to work with other APIs, so assert with the `as` keyword. This tells the compiler you know better than it does.
511
516
512
517
```tsx
513
518
classMyComponentextendsReact.Component<{
@@ -526,7 +531,9 @@ class MyComponent extends React.Component<{
526
531
527
532
<summary>Explanation</summary>
528
533
529
-
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
534
+
Note that this is [not the same as casting](https://www.reddit.com/r/reactjs/comments/8o5owb/react_typescript_cheatsheet_for_react_users_using/e01d2md/?context=3).
535
+
536
+
Something to add? Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
530
537
</details>
531
538
532
539
@@ -588,6 +595,10 @@ export const Checkbox = (
588
595
This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new) with your suggestions!
589
596
</details>
590
597
598
+
## Type Zoo
599
+
600
+
As you can see from the Omit example above, you can write significant logic in your types as well. [type-zoo](https://github.com/pelotom/type-zoo) is a nice toolkit of operators you may wish to check out (includes Omit).
601
+
591
602
# Troubleshooting Handbook: TSLint
592
603
593
604
Sometimes TSLint is just getting in the way. Judicious turning off of things can be helpful. Here are useful tslint disables you may use:
@@ -666,7 +677,10 @@ This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/r
666
677
# Other React + Typescript resources
667
678
668
679
- me! <https://twitter.com/swyx>
669
-
-<https://github.com/piotrwitek/react-redux-typescript-guide> - **HIGHLY HIGHLY RECOMMENDED**, i wrote this repo before knowing about this one, this has a lot of stuff I don't cover.
680
+
-<https://github.com/piotrwitek/react-redux-typescript-guide> - **HIGHLY HIGHLY RECOMMENDED**, i wrote this repo before knowing about this one, this has a lot of stuff I don't cover, including **REDUX** and **JEST**.
681
+
-[Ultimate React Component Patterns with Typescript 2.8](https://levelup.gitconnected.com/ultimate-react-component-patterns-with-typescript-2-8-82990c516935)
682
+
-[Basarat's Typescript gitbook has a React section](https://basarat.gitbooks.io/typescript/content/docs/jsx/react.html) with an Egghead.io course as well.
683
+
-[Charles Bryant's gitbook](https://charleslbryant.gitbooks.io/hello-react-and-typescript/content/) 2yrs old and on the more basic side but has sample code and IDE advice.
0 commit comments