我想要有条件的道具类型
当我尝试将默认值传递给位于条件类型内部的可选属性时,类型会中断
export type ChatBase = {
id: string;
title: string;
}
type ChatCardProps = CardProps &
(
| {
chat: ChatBase & { createdBy: { displayName: string } };
hideCreator?: false;
}
| {
chat: ChatBase;
hideCreator: true;
}
);
const Chat = ({ title, chat, hideCreator = false }: ChatCardProps) => {
return (
<View>
<Text>{title}</Text>
{!hideCreator && <Text>{chat.createdBy.displayName}</Text>}
// ^? createdBy?: { displayName: string; } | undefined
</View>
)
}
const Chat = ({ title, chat, hideCreator }: ChatCardProps) => {
return (
<View>
<Text>{title}</Text>
{!hideCreator && <Text>{chat.createdBy.displayName}</Text>}
// ^? createdBy?: { displayName: string; }
</View>
)
}
为什么当我传递默认值时逻辑会中断?