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

Typing Props Destructuring

Properly type destructured props in function parameters while maintaining type safety and intellisense.

Typing Props Destructuring

You'll learn how to maintain full type safety and autocomplete when destructuring props directly in function parameters.

The Core Idea

When you destructure props in the function signature itself (rather than inside the body), TypeScript needs the type annotation in a slightly different position. The key is annotating before the destructuring happens, so TypeScript knows what structure you're pulling apart.

Think of it like unpacking a labeled box: you need to read the label on the outside of the box before you start pulling items out, not after.

The Pattern

Instead of this:

// Type comes after destructuring - doesn't work!
function Button({ label, onClick }: { label: string; onClick: () => void }) {
  // ✅ This actually DOES work - correct syntax
}

Wait—that example is correct! The type annotation goes after the destructured parameter. Here's what matters:

interface ButtonProps {
  label: string;
  onClick: () => void;
  disabled?: boolean;
}

// ✅ Type annotation after the destructured param
function Button({ label, onClick, disabled = false }: ButtonProps) {
  return <button onClick={onClick} disabled={disabled}>{label}</button>;
}

The type annotation (ButtonProps) comes after the entire destructuring pattern. TypeScript infers each destructured variable's type from the interface. Default values work seamlessly—TypeScript knows disabled is boolean | undefined from the interface, and the default makes it always boolean inside the function.

// Also valid: inline type
function Alert({ message, variant }: { message: string; variant: 'info' | 'error' }) {
  // Full intellisense on message and variant
}

Key Takeaway: Place the type annotation after the destructuring syntax in the parameter list—TypeScript will infer types for each destructured property, preserving full type safety and autocomplete while keeping your syntax clean.