You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native TypeScript:为何出现双重is_verified?如何改为单次访问?

解决方法

有两种实用方案可以让你只需访问一次is_verified:

方案1:解构组件入参

在定义组件时直接解构参数对象,就能直接使用is_verified变量,无需通过对象嵌套访问:

interface CurrentUser {
  is_verified?: boolean;
  // 其他属性...
}

const Verified = ({ is_verified }: Pick<CurrentUser, 'is_verified'>) => {
  return <div>{is_verified ? '已验证' : '未验证'}</div>;
};

方案2:直接使用属性类型作为参数类型

如果组件仅依赖is_verified这个属性值,不需要接收整个对象,可以直接将参数类型指定为CurrentUser['is_verified']:

interface CurrentUser {
  is_verified?: boolean;
  // 其他属性...
}

const Verified = (is_verified?: CurrentUser['is_verified']) => {
  return <div>{is_verified ? '已验证' : '未验证'}</div>;
};

// 调用示例
<Verified is_verified={user.is_verified} />

内容的提问来源于stack exchange,提问作者universe11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:01:06