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
*[Higher Order Components](#higher-order-components-hocs)
18
19
*[Render Props](#render-props)
@@ -40,6 +41,88 @@
40
41
-[Section 4: @types/react and @types/react-dom APIs](#section-4-typesreact-and-typesreact-dom-apis)
41
42
</details>
42
43
44
+
# Section 0: Utility Types
45
+
46
+
Handy Utility Types used in the rest of this cheatsheet, or commonly used with React+TS apps, with explanation on what they do and how they can help. We will assume knowledge of [mapped types and conditional types](https://mariusschulz.com/blog/series/typescript-evolution) like `Exclude<T, U>` and `ReturnType<T>` but try to build progressively upon them.
47
+
48
+
<details>
49
+
<summary>
50
+
<code>Omit<T, K extends keyof T></code>: Subtract keys from one interface from the other.
51
+
</summary>
52
+
53
+
```ts
54
+
/**
55
+
* Subtract keys from one interface from the other.
<code>Optionalize<T extends K, K></code>: Remove from T the keys that are in common with K
80
+
</summary>
81
+
82
+
```ts
83
+
/**
84
+
* Remove from T the keys that are in common with K
85
+
*/
86
+
typeOptionalize<TextendsK, K> =Omit<T, keyofK>;
87
+
```
88
+
89
+
An example usage is in our HOC section below.
90
+
91
+
</details>
92
+
<details>
93
+
<summary>
94
+
<code>Nullable<T></code> or <code>Maybe<T></code>: Make a Type into a Maybe Type
95
+
</summary>
96
+
97
+
```ts
98
+
/**
99
+
* Make a Type into a Maybe Type
100
+
*/
101
+
typeNullable<T> =T|null
102
+
typeMaybe<T> =T|undefined
103
+
```
104
+
105
+
Your choice of `null` or `undefined` depends on your approach toward missing values. Some folks feel strongly one way or the other.
106
+
107
+
</details>
108
+
<details>
109
+
<summary>
110
+
<code>Dictionary<T></code>: Dictionary of string, value pairs
111
+
</summary>
112
+
113
+
```ts
114
+
/**
115
+
* Dictionary of string, value pairs
116
+
*/
117
+
typeDictionary<T> = { [key:string]:T }
118
+
```
119
+
120
+
`[key: string]` is a very handy trick in general. You can also modify dictionary fields with [Readonly](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html) or make them optional or Omit them, etc.
121
+
122
+
</details>
123
+
124
+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new). We respect the fact that naming and selection of examples here is arbitrary as the possible space is infinite.
125
+
43
126
# Section 1: Advanced Guides
44
127
45
128
## Higher Order Components (HoCs)
@@ -88,29 +171,6 @@ Now when consuming the component you can omit the `primaryColor` prop or overrid
88
171
89
172
**Declaring the HoC**
90
173
91
-
The following utilities will be needed.
92
-
93
-
```ts
94
-
/**
95
-
* Generic type utility to subtract keys from one interface from the other.
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
246
-
247
304
interface LinkProps {}
248
-
249
305
type AnchorProps = React.AnchorHTMLAttributes<HTMLAnchorElement>
250
306
type RouterLinkProps = Omit<NavLinkProps, 'href'>
251
307
@@ -274,8 +330,6 @@ const Link = <T extends {}>(
274
330
Ifyouwanttoconditionallyrenderacomponent, sometimesisbettertouse [React's composition model](https://reactjs.org/docs/composition-vs-inheritance.html) to have simpler components and better to understand typings:
275
331
276
332
```tsx
277
-
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
278
-
279
333
type AnchorProps = React.AnchorHTMLAttributes<HTMLAnchorElement>
0 commit comments