Course contentsShow
← React
Lesson 2336 of 2,39651. TypeScript with React BasicsFree lesson

Extending Native HTML Element Props

Combine custom props with native HTML attributes using intersection types and extends.

Extending Native HTML Element Props

You'll learn how to create type-safe custom components that accept all native HTML attributes plus your own custom props.

The Core Idea

Sometimes you build a custom Button or Input component but want it to accept all the normal HTML attributes (onClick, disabled, aria-label, etc.) plus your custom props (like variant or loading). Instead of manually typing every HTML attribute, TypeScript lets you extend the native element's type and add your extras.

This combines intersection types (&) or the extends keyword with React's built-in types like React.ButtonHTMLAttributes<HTMLButtonElement>.

Two Patterns: Intersection and Extends

Pattern 1: Intersection Type (&)

interface CustomButtonProps {
  variant: 'primary' | 'secondary';
  loading?: boolean;
}

type ButtonProps = CustomButtonProps & React.ButtonHTMLAttributes<HTMLButtonElement>;

function Button({ variant, loading, ...rest }: ButtonProps) {
  return <button className={variant} disabled={loading} {...rest} />;
}

// Usage: accepts variant + all native button props
<Button variant="primary" onClick={handleClick} aria-label="Save" />

Pattern 2: Interface Extends

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  variant: 'primary' | 'secondary';
  loading?: boolean;
}

Both approaches work identically. The component now accepts onClick, disabled, type, className, and every other native button attribute, plus your custom variant and loading props. TypeScript validates everything.

Why This Matters

Without extending native props, users can't pass aria-* attributes or event handlers without TypeScript errors. By extending, your component becomes a true enhancement of the native element, not a replacement.

Key Takeaway: Use intersection types (&) or extends with React.HTMLAttributes types to merge custom props with all native HTML element attributes, creating flexible, type-safe components.