我的反应应用程序中有下一个情况,我使用打字稿。我有两种类型:
type Square = {
size: number;
}
type Rectangle = {
width: number;
height: number;
}
当我想作为道具发送如下值时:
// const data: Square | Rectangle = squareVariable || rectangleVariable
<Component data={data.size || data.width} />
当我尝试这样做时,我收到了 Property 'size' does not exist on type 'Rectangle'
和 Property 'width' does not exist on type 'Square '
的下一个错误。
如何解决问题?