TypeScript adds type safety to React. Define prop interfaces: interface Props { title: string; count?: number }. Type events as React.ChangeEvent<HTMLInputElement>. Errors show in the editor before running code.
React with TypeScript
npm create vite@latest my-app -- --template react-ts
interface User {
id: number;
name: string;
email: string;
}
interface Props {
user: User;
onSelect: (id: number) => void;
}
function UserCard({ user, onSelect }: Props) {
return (
onSelect(user.id)}>
{user.name}
);
}