Typing Component Props, Events, and Hooks

TypeScript really shines in React when working with component props, events, and hooks like useState or useEffect. Here’s how to type them properly so you get type safety and helpful autocompletions.

Typing Component Props

interface Todo {
  title: string;
  done: boolean;
}

interface TodoItemProps {
  todo: Todo;
  onToggle: (title: string) => void;
}

const TodoItem: React.FC<TodoItemProps> = ({ todo, onToggle }) => (
  <div>
    <input
      type="checkbox"
      checked={todo.done}
      onChange={() => onToggle(todo.title)}
    />
    {todo.title}
  </div>
);

💡 Tip: Try it in your IDE!

With typed props, you’ll get autocompletion and instant feedback — making mistakes harder and coding faster.

Typing useState

Step 1: Create the state type

Step 2: Type the useState hook

Now todos is typed as an array of Todo objects, and setTodos will only accept that type.

Step 3: Using setTodos

Here are three valid and clean ways to use setTodos:

🧠 Why use the callback form?

React batches updates — using the callback ensures you’re working with the most recent state, especially inside useEffect or async operations.

Typing Events

React’s synthetic events are strongly typed too. Here’s an example with an input change handler:

Typing Custom Hooks

If you create your own hooks, type the input and output clearly:

Last updated