0.1.3 • Published 5 years ago
props-of v0.1.3
Deprecated
You don't need to install anything.
Use React.ComponentProps\<T> defined in @types/react
😉
Infer types of props from React component
Contents
📦 How to get the code?
This is package is one line of code, so you can either install it or copy-paste it 😉
🔌 Installation
yarn add --dev props-of
📃 Source
export type PropsOf<T> = T extends React.ComponentType<infer P> ? P : never;
❔ Why
And now we can use it for props of parent component.
type MenuLinkProps = {
to: LinkProps['to'];
} & PropsOf<typeof Menu.Item>;
const MenuLink = ({ children, to, ...rest }: MenuLinkProps) => (
<Menu.Item {...rest}>
<Link to={to}>{children}</Link>
</Menu.Item>
);
📚 I want to know more
You can learn about infer
and conditional types at \
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html