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
function Component({ className, children, text }:Props) {
27
-
//...
30
+
function Component({ className, children, text, ...props }:Props) {
31
+
//`Props` includes `text` in addition to all valid `div` props
28
32
}
29
33
```
30
34
@@ -45,13 +49,19 @@ type MyType = ComponentProps<typeof Component>;
45
49
// ^? type MyType = Props
46
50
```
47
51
48
-
The type of a specific prop can also be inferred this way. Let's say you are using an `<Icon>` component from a component library. The component takes a `name` prop that determines what icon is shown. You need to use the type of `name` in your app, but it's not made available by the library. No problem!
52
+
#### Inferring specific prop type
53
+
54
+
The type of a specific prop can also be inferred this way. Let's say you are using an `<Icon>` component from a component library. The component takes a `name` prop that determines what icon is shown. You need to use the type of `name` in your app, but it's not made available by the library. You could create a custom type:
55
+
56
+
```tsx
57
+
typeIconName="warning"|"checkmark";
58
+
```
59
+
60
+
However, this type if not really reflecting the actual set of icons made available by the library. A better solution is to infer the type:
49
61
50
62
```tsx
51
63
import { Icon } from"component-library";
52
64
53
65
typeIconName=ComponentProps<typeofIcon>["name"];
54
66
// ^? type IconName = "warning" | "checkmark" | ...
55
67
```
56
-
57
-
Needless to say, `ComponentProps<T>` can be very useful!
0 commit comments