Typing React Components in TypeScript (Practical Patterns)
Props, children, event handlers, generic components and polymorphic `as` props in React + TypeScript, with the pitfalls interviewers ask about.
On this page
30‑second interview answer
In React + TypeScript, type your component props with an interface/type, prefer explicit prop types, and rely on inference for JSX. Use correct event types (React.ChangeEvent, etc.), avoid overusing React.FC, and use generics when building reusable components. Keep types simple and ergonomic.
Typing props
type ButtonProps = {
variant?: 'primary' | 'secondary';
onClick?: () => void;
children: React.ReactNode;
};
function Button({ variant = 'primary', onClick, children }: ButtonProps) {
return <button onClick={onClick}>{children}</button>;
}Events
function Input() {
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
console.log(e.target.value);
};
return <input onChange={onChange} />;
}Generics in components
type SelectProps<T> = {
items: T[];
getKey: (item: T) => string;
getLabel: (item: T) => string;
};
function Select<T>({ items, getKey, getLabel }: SelectProps<T>) {
return (
<ul>
{items.map((item) => (
<li key={getKey(item)}>{getLabel(item)}</li>
))}
</ul>
);
}The caller decides what T is, and TypeScript checks getKey and getLabel against it. Taking a getKey instead of using the array index keeps keys stable when the list is filtered or reordered (see React keys and remounting).
Polymorphic components: the as prop
Design-system primitives often let the caller change the underlying element:
<Button onClick={() => {}}>Save</Button>
<Button as="a" href="/pricing">Pricing</Button>
// <Button as="button" href="/x" /> // should be a type errorThe typing problem is making the allowed props depend on as: href is valid for as="a" but not for a button. The maintainable pattern is a generic element type plus ComponentPropsWithoutRef<T>:
import type { ElementType, ComponentPropsWithoutRef, ReactNode } from "react";
type PolymorphicProps<T extends ElementType, OwnProps> = OwnProps & {
as?: T;
} & Omit<ComponentPropsWithoutRef<T>, keyof OwnProps | "as">;
type ButtonOwnProps = {
variant?: "primary" | "secondary";
children?: ReactNode;
};
export function Button<T extends ElementType = "button">(
props: PolymorphicProps<T, ButtonOwnProps>
) {
const { as, variant = "primary", ...rest } = props;
const Comp = (as ?? "button") as ElementType;
return <Comp data-variant={variant} {...rest} />;
}- The component inherits the intrinsic props of whatever
asis. Omitremoves collisions with your own props, sovariantcan't be overridden by an HTML attribute of the same name.- Casting
ComptoElementTypeis the one deliberate escape hatch. JSX can't check a spread against a generic element, and the public props type is still strict.
For a full design-system version you would also forward refs (forwardRef, ComponentPropsWithRef<T> and a small PolymorphicRef<T> helper). For most teams the version above is the right trade-off: correct props for callers and types the team can still read.
Common pitfalls
- Using
anyfor events - Overcomplicating with
React.FC(it makes generic and polymorphic components harder, not easier) - Using index keys in lists
- Hand-writing a union of element props (
button | a | div | ...) for anasprop. It doesn't scale. - Typing everything as
HTMLAttributes<HTMLElement>, which loseshrefandtypechecking
Mini Q&A
Q1: Should you use React.FC?
- Not required; many teams avoid it.
Q2: How do you type children?
React.ReactNode.
Q3: When do you need generics?
- reusable typed components.
Q4: How do you make props depend on an as prop?
- Make the element type generic (
T extends ElementType) and intersect your own props withOmit<ComponentPropsWithoutRef<T>, keyof OwnProps | "as">.
Q5: What's the main risk with polymorphic types?
- Making them so clever that the team can't maintain them. Ship the simple version that gets props right.
Summary checklist
- I can type props and events.
- I avoid overusing React.FC.
- I can write a generic component.
- I can type an
asprop so its props follow the chosen element.
Looking for structured, offline interview prep?
Free online lessons • Portable Amazon Kindle / Paperback
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate I earn from qualifying purchases.
Written by Rajesh Dhiman
Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.