The Type Safety Problem
You'll learn: Why spreading unknown props can silently break TypeScript's safety guarantees and cause runtime errors.
The Hidden Danger of Spreading Props
When you spread props onto a component or element using {...props}, you're essentially saying "pass along whatever you received." This seems convenient, but there's a catch: you lose control over what gets passed through.
TypeScript can't verify that the props being spread are valid for the target. If your wrapper component receives unexpected or mistyped props, spreading them blindly bypasses all type checking.
A Concrete Example
Imagine wrapping a <button> element:
function CustomButton(props: any) {
return <button {...props} />;
}
// Later, someone passes invalid props:
<CustomButton onKlick={() => {}} /> // Typo! Should be onClick
TypeScript won't catch the typo because props is typed as any. The button renders, but the click handler never fires. You've lost type safety and broken functionality.
Why This Matters
When you spread props without proper typing:
- Invalid prop names slip through
- Wrong prop types go undetected
- Required props might be missing
- You can't see what props are actually supported
It's like accepting a box of unknown items and handing it to someone else without checking the contents first.
Key Takeaway: Spreading props without explicit TypeScript types creates a "trust gap" where invalid props pass through unchecked, leading to silent runtime failures that TypeScript was supposed to prevent.