Hello! Welcome back to our module on Advanced Component and TypeScript Patterns.
In our previous lesson, we explored how to use TypeScript generics to build highly reusable components, like a GenericList that could handle any data type while maintaining full type safety. We learned to make our components flexible in the data they consume.
Today, we're going to tackle flexibility from a different angle: the component's props themselves. We'll learn how to manipulate and combine existing type definitions to create powerful and flexible prop interfaces without manually re-typing everything. Our goal is to utilize TypeScript utility types (e.g., Pick, Omit, ComponentProps) to create flexible prop types. This skill is fundamental to writing clean, maintainable, and scalable component APIs in a professional React and TypeScript environment.
The DRY Principle for Types
In software engineering, you're familiar with the "Don't Repeat Yourself" (DRY) principle for code. This principle is just as important for types. Manually defining props that already exist elsewhere (like in another component or a native HTML element) leads to maintenance headaches. If the source type changes, you have to hunt down and update every copy.
TypeScript utility types are the solution. They are functions for types, allowing you to create new types by transforming existing ones. Let's start by understanding two of the most common utility types: Pick and Omit.
You are a Junior Dev if You Don’t Know These 18 TypeScript Utility Types
This video from Web Dev Simplified provides a clear, practical introduction to several utility types using a simple User object example. It's a great way to grasp the core concepts before we apply them to React.
Please watch the following segments: The Problem (00:40 - 01:46): Understand why passing a full User object to every function is inflexible. Pick (01:46 - 03:43): See how Pick<User, 'name' | 'age'> creates a new type with only the selected properties. Omit (03:43 - 04:56): Learn how Omit<User, 'id'> creates a new type by removing specified properties.
As the video demonstrates:
Pick<Type, Keys>: Creates a new type by picking a set of properties (Keys) from an existingType.Omit<Type, Keys>: Creates a new type by taking all properties fromTypeand then removing a set of properties (Keys).
This image provides a great visual summary of Pick and Omit.

Now, let's see how we can apply these concepts to make our React components more robust.
The Wrapper Component Problem
A very common task in React is creating a custom component that acts as a "wrapper" around a standard HTML element. For example, you might create a <Button> component to ensure consistent styling across your app.
The challenge is this: how do you ensure your custom <Button> can accept all the standard props a native <button> element can, like onClick, disabled, type, and various aria-* attributes, without manually typing every single one?
Mirroring DOM Props with ComponentPropsWithoutRef
This article, 'Mirroring DOM Props with ComponentPropsWithoutRef', perfectly frames this problem and introduces the elegant solution provided by React's built-in types.
Read the sections 'The Problem: Manual Prop Forwarding' and 'The Solution: ComponentPropsWithoutRef'. Focus on the contrast between the manual, brittle approach and the clean, maintainable solution.
Using React.ComponentProps
As you just read, React provides a utility type specifically for this purpose. The most common variant you'll use is React.ComponentPropsWithoutRef.
ComponentPropsWithoutRef<'element'> extracts all the prop types from a given native HTML element, excluding the ref prop.
Let's see this in action. The following video walks through building a custom button and demonstrates exactly how to use this utility type.
React TypeScript Tutorial - 22 - Wrapping HTML Elements
This video from Codevolution provides a step-by-step guide to wrapping HTML elements in React with TypeScript.
Watch from the beginning to 05:08. The first part (until 03:46) sets up the problem, and the second part shows how to solve it by extending React.ComponentProps.
Let's formalize the pattern. To create a custom button that has a special variant prop but also accepts all standard button attributes, you would define your props like this:
import type { ComponentPropsWithoutRef } from 'react';
// Define our custom props
type CustomButtonProps = {
variant: 'primary' | 'secondary';
};
// Combine our custom props with all native button props
type ButtonProps = CustomButtonProps & ComponentPropsWithoutRef<'button'>;
export const Button = ({ variant, className, children, ...rest }: ButtonProps) => {
// `rest` now contains all other native button props like `onClick`, `disabled`, etc.
// TypeScript knows their types perfectly.
const variantClass = `btn-${variant}`;
return (
<button className={`${variantClass} ${className}`} {...rest}>
{children}
</button>
);
};
With this, your <Button> is now as flexible as a native <button> element, with full IntelliSense and type-checking for all standard attributes.
Handling Prop Conflicts with Omit
What happens when your custom prop has the same name as a native HTML attribute? For instance, you might want to create your own children prop that only accepts a string, overriding the default React.ReactNode.
This is where Omit becomes incredibly useful. You can use it to remove a specific prop from the ComponentProps before adding your own version.
Let's return to the Codevolution video to see exactly how this is done.
React TypeScript Tutorial - 22 - Wrapping HTML Elements
The final part of the video demonstrates how to use the Omit utility type to override a native prop.
Watch from 05:08 to the end (07:30). Pay close attention to how Omit is used to remove the original children prop from the button's type, allowing for a new, more restrictive definition.
The pattern looks like this:
import type { ComponentPropsWithoutRef } from 'react';
// Let's say we want to replace the native `onClick` with our own.
// The native `onClick` is `(event: React.MouseEvent<HTMLButtonElement>) => void`.
// Our custom `onClick` will be `(someId: string) => void`.
type CustomButtonProps = {
// Our custom onClick signature
onClick: (someId: string) => void;
someId: string;
};
// 1. Get all button props, but...
// 2. ...OMIT the original 'onClick' prop.
// 3. Then, intersect (&) with our custom props.
type ButtonProps = CustomButtonProps & Omit<ComponentPropsWithoutRef<'button'>, 'onClick'>;
export const Button = ({ onClick, someId, children, ...rest }: ButtonProps) => {
// The `onClick` here is now our custom one.
const handleClick = () => {
onClick(someId);
};
return (
// We pass our new handler to the native `onClick`.
<button onClick={handleClick} {...rest}>
{children}
</button>
);
};
This pattern gives you complete control, allowing you to "borrow" the vast majority of props from a native element while precisely overriding the few that you need to customize.
Test your understanding!
You need to create a custom <Input> component with a label. Your task is to define the InputProps type.
It must satisfy these requirements:
- It should accept all props that a standard HTML
<input>element can accept. - It must include a new, required
labelprop of typestring. - The native
sizeattribute of an<input>(which defines width in characters) can conflict with a common design system propsize(e.g.,'small' | 'medium'). To avoid this, you should remove the nativesizeprop from your component's accepted props.
How would you define InputProps?
Show answer
Here is the solution:
import type { ComponentPropsWithoutRef } from 'react';
// 1. Define the custom props.
type CustomInputProps = {
label: string;
};
// 2. Omit the 'size' prop from the native input props,
// and then combine with our custom props.
type InputProps = CustomInputProps & Omit<ComponentPropsWithoutRef<'input'>, 'size'>;
// Example component implementation:
const MyInput = ({ label, ...rest }: InputProps) => {
return (
<div>
<label>{label}</label>
<input {...rest} />
</div>
);
};
This InputProps type correctly inherits all <input> attributes, removes the conflicting size prop, and adds the required label prop.
Conclusion
Mastering utility types is a significant step toward writing professional-grade React components. You've unlocked the ability to create flexible, maintainable, and highly expressive component APIs.
Key Takeaways:
- DRY for Types: Utility types like
PickandOmitallow you to create new types by transforming existing ones, keeping your type definitions clean and maintainable. - Wrapper Component Superpowers:
React.ComponentPropsWithoutRefis the go-to utility for creating wrapper components that inherit all the props of a native HTML element, providing excellent developer experience and type safety. - Precise Overrides: Combining
ComponentPropsWithoutRefwithOmitis a powerful pattern that allows you to borrow a comprehensive set of native props while overriding specific ones to fit your component's unique API.
Preview of the Next Lesson:
We've learned to make components generic and to make their props flexible. In our next lesson, we will combine these ideas to tackle an even more advanced pattern: polymorphic components. We'll use TypeScript's discriminated unions to build components that can change their behavior—and even the HTML element they render—based on a specific prop, all while maintaining perfect type safety.