TypeScript-React 基础类型示例指南:组件 Props 的类型定义

TypeScript-React 基础类型示例指南:组件 Props 的类型定义

react Cheatsheets for experienced React developers getting started with TypeScript react 项目地址: https://gitcode.com/gh_mirrors/rea/react

前言

在 React 与 TypeScript 的结合使用中,正确地为组件 Props 定义类型是开发高质量应用的基础。本文将系统性地介绍在 React+TypeScript 项目中常见的 Props 类型定义方式,帮助开发者构建类型安全的组件。

基础 Props 类型示例

以下是一个包含了 React+TypeScript 应用中常见类型的完整示例:

type AppProps = {
  // 基本类型
  message: string;
  count: number;
  disabled: boolean;
  
  // 数组类型
  names: string[];
  
  // 字符串字面量联合类型
  status: "waiting" | "success";
  
  // 对象类型
  obj: {
    id: string;
    title: string;
  };
  
  // 对象数组
  objArr: {
    id: string;
    title: string;
  }[];
  
  // 函数类型
  onClick: () => void;
  onChange: (id: number) => void;
  
  // React 事件处理函数
  onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
  onClick(event: React.MouseEvent<HTMLButtonElement>): void;
  
  // 可选属性
  optional?: OptionalType;
  
  // useState 的 setter 函数
  setState: React.Dispatch<React.SetStateAction<number>>;
};

对象类型的注意事项

在 TypeScript 中,object 类型表示"任何非原始类型",包括对象、数组、函数等,但不包括 number, string, boolean, symbol, nullundefined

在实际 React 开发中,直接使用 object 类型的情况较少,因为它过于宽泛,失去了类型检查的意义。更推荐使用具体的对象形状定义。

React 特有 Props 类型

对于接收其他 React 组件作为 Props 的情况,有以下实用类型:

export interface AppProps {
  // 最通用的子元素类型,接受任何 React 可以渲染的内容
  children?: React.ReactNode;
  
  // 单个 React 元素
  childrenElement: React.JSX.Element;
  
  // 样式属性
  style?: React.CSSProperties;
  
  // 表单事件
  onChange?: React.FormEventHandler<HTMLInputElement>;
  
  // 组合组件 Props
  props: Props & React.ComponentPropsWithoutRef<"button">;
  props2: Props & React.ComponentPropsWithRef<MyButtonWithForwardRef>;
}

React.ReactNode 与 React.JSX.Element 的区别

  • React.JSX.ElementReact.createElement 的返回值类型
  • React.ReactNode 是组件可能返回的所有类型的集合

简单来说,React.ReactNodeReact.JSX.Element 更广泛,包含了更多可能的返回值类型。

类型(Type)与接口(Interface)的选择

在定义 Props 和 State 时,开发者常面临选择 type 还是 interface 的困惑。

实用建议

  1. 公共 API 定义:使用 interface,因为它支持声明合并
  2. 组件 Props 和 State:考虑使用 type,因为它更受约束
  3. 联合类型:必须使用 type
  4. 扩展实现:优先使用 interface

类型与接口对比表

| 特性 | Type | Interface | |--------------------------|------|-----------| | 描述函数 | ✅ | ✅ | | 描述元组 | ✅ | ✅ | | 扩展接口 | ⚠️ | ✅ | | 类实现(implements) | ⚠️ | ✅ | | 创建联合类型 | ✅ | ❌ | | 创建映射类型 | ✅ | ❌ | | 声明合并 | ❌ | ✅ |

⚠️ 表示在某些情况下可用

最佳实践总结

  1. 优先为 Props 定义明确的类型,避免使用过于宽泛的类型如 anyobject
  2. 事件处理函数使用 React 提供的特定事件类型
  3. 子元素类型根据实际需要选择 React.ReactNodeReact.JSX.Element
  4. 遵循团队约定选择 typeinterface,保持项目一致性
  5. 对于可重用类型定义,考虑使用 interface 以便扩展

通过合理运用这些类型定义技巧,可以显著提高 React+TypeScript 项目的代码质量和开发体验。

react Cheatsheets for experienced React developers getting started with TypeScript react 项目地址: https://gitcode.com/gh_mirrors/rea/react

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲍诚寒Yolanda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值