因此,我正在尝试编写一种道具类型格式,其中如果选择了一个道具,则将丢弃另一个道具。
type ButtonProps = {
to: string,
} | {
onClick: (() => void)
};
export const BackButton = (props: ButtonProps) => {
if(props.to != null) {
//props {to} will be used hence no need for onClick
}
else {
// {onClick} will be used over {to}
}
}
但它说
类型“ButtonProps”上不存在属性“to”。类型 '{ onClick: () => void; 上不存在属性 'to' }'.ts(2339`
如何用左右的方式格式化形状时,当一个选择两个都将被丢弃。没有可选项,选择的道具是必需的。