diff --git a/docs/react-types/ComponentProps.md b/docs/react-types/ComponentProps.md new file mode 100644 index 00000000..50ed42a8 --- /dev/null +++ b/docs/react-types/ComponentProps.md @@ -0,0 +1,76 @@ +--- +title: ComponentProps +--- + +`ComponentProps` constructs a type with all valid props of an element or inferred props of a component. + +:::note + +Prefer `ComponentPropsWithRef` if ref is forwarded and `ComponentPropsWithoutRef` when ref is not forwarded. + +::: + +## Parameters + +- `ElementType`: An element type. Examples include: + - An HTML or SVG element string literal such as `"div"`, `"h1"` or `"path"`. + - A component type, such as `typeof Component`. + +## Usage + +### Get all valid props of an element + +`ComponentProps` can be used to create a type that includes all valid `div` props. + +```tsx +interface Props extends ComponentProps<"div"> { + text: string; +} + +function Component({ className, children, text, ...props }: Props) { + // `props` includes `text` in addition to all valid `div` props +} +``` + +### Infer component props type + +In some cases, you might want to infer the type of a component's props. + +```tsx +interface Props { + text: string; +} + +function Component(props: Props) { + // ... +} + +type MyType = ComponentProps; +// ^? type MyType = Props +``` + +#### Infer specific prop type + +The type of a specific prop can also be inferred this way. Let's say you are using an `` 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: + +```tsx +type IconName = "warning" | "checkmark"; +``` + +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: + +```tsx +import { Icon } from "component-library"; + +type IconName = ComponentProps["name"]; +// ^? type IconName = "warning" | "checkmark" +``` + +You can also use the `Pick` utility type to accomplish the same thing: + +```tsx +import { Icon } from "component-library"; + +type IconName = Pick, "name">; +// ^? type IconName = "warning" | "checkmark" +``` diff --git a/docs/react-types/index.md b/docs/react-types/index.md new file mode 100644 index 00000000..12171892 --- /dev/null +++ b/docs/react-types/index.md @@ -0,0 +1,7 @@ +--- +title: React Types +--- + +`@types/react` makes some types available that can be very useful. Here's a list in alphabetical order with links to the detailed reference pages. + +- [`ComponentProps`](/docs/react-types/ComponentProps)