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
Tweak explanations of object types in Basic Prop Types Examples (#582)
* Reorder and reword some of the basic prop type examples
Put the recommended approaches before the non-recommended approaches, and reword some of the decriptions of object types
* Add details section with more description of object types
* Add note about Map
* Update README.md
/** any function as long as you don't invoke it (not recommended) */
1127
+
onSomething:Function;
1127
1128
/** an optional prop (VERY COMMON!) */
1128
1129
optional?:OptionalType;
1129
1130
/** when passing down the state setter function returned by `useState` to a child component. `number` is an example, swap out with whatever the type of your state */
@@ -1133,6 +1134,17 @@ type AppProps = {
1133
1134
1134
1135
Notice we have used the TSDoc `/** comment */` style here on each prop. You can and are encouraged to leave descriptive comments on reusable components. For a fuller example and discussion, see our [Commenting Components](https://react-typescript-cheatsheet.netlify.app/docs/advanced/misc_concerns/#commenting-components) section in the Advanced Cheatsheet.
1135
1136
1137
+
<details>
1138
+
<summary>More on object types: <code>object</code>, <code>{"{}"}</code>, etc</summary>
1139
+
1140
+
In Typescript, it's generally best to use specific types for objects. In most cases, this means a literal type like <code>{ id: string; name: string }</code>. In cases where there isn't a fixed structure for an object, you likely either want an index signature (possibly with the <code>Record</code> shorthand) - if there are values of a certain type, but the keys can change - or else <ahref="https://www.typescriptlang.org/docs/handbook/2/generics.html">generics</a> - if the object structure is more-or-less an arbitrary black-box.
1141
+
1142
+
Another approach to objects is the <code>Map</code> data structure, but this is somewhat uncommon to use in React, because React prefers data to be changed immutably (e.g. <code>setUser({...user, name: newName})</code>), while Maps are mutable data structures.
1143
+
1144
+
"Vague" object types like <code>object</code>, <code>{}</code> are fairly niche and should be rarely used, and may function differently than you expect. <code>object</code> is any non-primitive value: this includes things like functions and arrays and constructors, not just "simple" objects. And <code>{}</code> is perhaps better thought of as "an interface with no required properties", not "an empty object" - in practice this type allows anything except <code>null</code> or <code>undefined</code>. <code>Object</code> behaves the same as <code>{}</code> and is basically never used.
1145
+
1146
+
</details>
1147
+
1136
1148
#### Useful React Prop Type Examples
1137
1149
1138
1150
Relevant for components that accept other React components as props.
/** any function as long as you don't invoke it (not recommended) */
49
+
onSomething:Function;
49
50
/** an optional prop (VERY COMMON!) */
50
51
optional?:OptionalType;
51
52
/** when passing down the state setter function returned by `useState` to a child component. `number` is an example, swap out with whatever the type of your state */
@@ -55,6 +56,17 @@ type AppProps = {
55
56
56
57
Notice we have used the TSDoc `/** comment */` style here on each prop. You can and are encouraged to leave descriptive comments on reusable components. For a fuller example and discussion, see our [Commenting Components](https://react-typescript-cheatsheet.netlify.app/docs/advanced/misc_concerns/#commenting-components) section in the Advanced Cheatsheet.
57
58
59
+
<details>
60
+
<summary>More on object types: <code>object</code>, <code>{"{}"}</code>, etc</summary>
61
+
62
+
In Typescript, it's generally best to use specific types for objects. In most cases, this means a literal type like <code>{ id: string; name: string }</code>. In cases where there isn't a fixed structure for an object, you likely either want an index signature (possibly with the <code>Record</code> shorthand) - if there are values of a certain type, but the keys can change - or else <ahref="https://www.typescriptlang.org/docs/handbook/2/generics.html">generics</a> - if the object structure is more-or-less an arbitrary black-box.
63
+
64
+
Another approach to objects is the <code>Map</code> data structure, but this is somewhat uncommon to use in React, because React prefers data to be changed immutably (e.g. <code>setUser({...user, name: newName})</code>), while Maps are mutable data structures.
65
+
66
+
"Vague" object types like <code>object</code>, <code>{}</code> are fairly niche and should be rarely used, and may function differently than you expect. <code>object</code> is any non-primitive value: this includes things like functions and arrays and constructors, not just "simple" objects. And <code>{}</code> is perhaps better thought of as "an interface with no required properties", not "an empty object" - in practice this type allows anything except <code>null</code> or <code>undefined</code>. <code>Object</code> behaves the same as <code>{}</code> and is basically never used.
67
+
68
+
</details>
69
+
58
70
## Useful React Prop Type Examples
59
71
60
72
Relevant for components that accept other React components as props.
0 commit comments