React Compound Component Typings
Define Composition Typings and Export Compound Components in React using TypeScript

Search for a command to run...
Define Composition Typings and Export Compound Components in React using TypeScript

No comments yet. Be the first to comment.
This blog post is fourteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 14. This challenge was about optimizing an algorithm that generates exponentially larger and larger strings. I found ...

This blog post is thirteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 13. All solutions are in this GitHub repository. Solving Part One Given a list of x, y coordinates representing dots ...

This blog post is twelfth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described on Day 12. All solutions are in this GitHub repository. Solving Part One Given a set of connections between underground caves, ...

This blog post is eleventh in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 11. This day had many similarities to Day 9, and I solved the challenge using similar methods. All solutions are in thi...

This blog post is the tenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 10. All solutions are in this GitHub repository. Solving Part One Given a set of lines with opening and closing bracke...

Imagine you were building a compound component in React.
<Tabs>
<Tabs.Title>Admin</Tabs.Title>
<Tabs.Item>Overview</Tabs.Item>
<Tabs.Item>Settings</Tabs.Item>
</Tabs>
The type of Tabs is FunctionComponent<TabsProps> so how can we export the Title and Item components?
We can define a TabsComposition interface:
interface TabsComposition {
Title: FunctionComponent<TitleProps>;
Item: FunctionComponent<ItemProps>;
}
Then the declaration of Tabs becomes:
const Tabs: FunctionComponent<TabsProps> & TabsComposition = ({children, ...props}) => (
<>{children}</>
);
We can then assign the Title and Item components like you would normally work with objects in JavaScript:
Tabs.Title = Title;
Tabs.Item = Item;
Finally, you can export the Tabs component:
export {Tabs};