Typed Props Typed Props Contracts

Last updated: 2026-11-02

Typed Props Typed Props Contracts

In TypeScript you use interface or type to build a contract for props: wrong names, missing props and mismatched types are all caught at compile time, and the editor gains auto-completion for free. Annotate the component parameter with a shape and the runtime cost stays at zero. The contract also documents the component for the next reader, so the API of a component is written down in one place.

GroupData & Engineering
KindPattern

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

type User = { id: number; name: string; vip?: boolean }  // ? marks an optional property

type UserCardProps = {
  user: User
  onSelect: (id: number) => void     // callback: annotate both the parameters and the return type
  children?: React.ReactNode         // any renderable content
}

function UserCard({ user, onSelect }: UserCardProps) {
  return <li onClick={() => onSelect(user.id)}>{user.name}</li>
}

// ❌ Compile error: <UserCard user={{ id: 'x' }} />  id should be a number

Returns:No return value; this is a coding pattern rather than a callable API.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

The table shows the props contract of UserCard; clicking a user triggers the onSelect callback and prints its id - in a real TypeScript project a wrong type would fail at compile time (runtime JSX demo).

💡 Key Points

💡 Tip Derive the props type from the component with React.ComponentProps<typeof UserCard> instead of maintaining the same definition twice.
ℹ️ Info React.ReactNode is the most general type for children (elements, strings and arrays are all accepted); use React.ReactElement when only elements are allowed.
⚠️ Warning any turns the checking off; when the shape is uncertain use unknown and narrow it down, or refine it step by step with utility types such as Partial and Pick.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Should I use type or interface for props?
A Both work. An interface can be declaration merged and reports errors more gracefully, while a type can express unions and combinations of utility types. Just be consistent across the team.
Q How do optional props and default values work together?
A vip?: boolean allows the prop to be omitted, and a default in the destructuring function X({ vip = false }: Props) gives it a fallback; the two line up.
Q Can TypeScript validate at runtime?
A No, TypeScript only works at compile time. For runtime validation of API data use zod: z.object({...}).parse(data) validates the payload and infers the type.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏