reactjs - React,TypeScript 条件道具
问题描述
我正在尝试使用两个接口之间的管道类型创建一个带有条件道具的 React 组件。我的设置是这样的:
interface BaseProps {
"aria-label"?: string;
className?: string;
onClick?: () => void;
}
interface IconProps extends BaseProps {
"aria-label": string;
children: never;
icon: React.ReactElement;
}
interface ButtonProps extends BaseProps {
children: React.ReactElement;
}
type Props = ButtonProps | IconProps;
class Button extends React.Component<Props> {
render(): React.ReactElement {
const { children, icon, ...rest } = this.props;
// ERROR: "Property 'icon' does not exist on type '(Readonly<ButtonProps> & Readonly<{ children?: ReactNode; }>) | (Readonly<IconProps> & Readonly<{ children?: ReactNode; }>)'."
}
}
我似乎无法弄清楚如何实现以下目标:
- 当有“icon”道具时,强制“aria-label”并禁止孩子
- 当没有“icon”道具时,强制孩子并允许可选的“aria-label”
解决方案
一种方法是在两个变体之间建立一个联合:
interface VariantWithIconProps {
icon: React.ReactElement,
"aria-label": string;
}
interface VariantNoIconProps {
"aria-label"?: string;
children: React.ReactNode,
}
type Props = VariantWithIconProps | VariantNoIconProps;
const iconProps: VariantWithIconProps = {
icon: <svg/>,
"aria-label": "a",
};
const props1: Props = iconProps;
const props2: VariantNoIconProps = iconProps; // typescript error
const noIconProps1: VariantNoIconProps = {
children: <div/>
};
const props3: Props = noIconProps1;
const props4: VariantWithIconProps = noIconProps1; // typescript error
const noIconProps2: VariantNoIconProps = {
children: <div/>,
'aria-label': "a"
};
const props5: Props = noIconProps2;
const props6: VariantWithIconProps = noIconProps2; // typescript error
不幸的是,如果没有变通方法,似乎很难在界面中禁止属性。
推荐阅读
- excel - 如何从电子表格中提取数据并使用该值?节点
- mongodb - 在 M1 Apple 芯片上安装 mongodb 时无法点击 mongodb/brew:tap 中的语法无效
- python - python XLRD库如何扫描excel中的所有工作表?
- java - 覆盖具有相同类策略与策略实例的 equals 方法
- sql - 在 SQL Server 数据库中查找以小写字母开头的列并将它们列为 SELECT 语句
- php - PHP Elastic Search - 在执行搜索之前获取所有文档(以节省时间/性能)
- c - 如何实现“malloc”和“fgets”来读取和打印输入流
- python - 如何在数据着色器热图中填充或插入稀疏数据空白空间(欠采样)?
- amazon-s3 - EMR Spark 无法将小型 PySpark 数据帧写入 S3
- flutter - Flutter:带有下划线的电话号码的预屏蔽输入