打字稿中的联合

时间:2021-05-17 16:28:00

标签: javascript reactjs typescript

我的反应应用程序中有下一个情况,我使用打字稿。我有两种类型:

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 ' 的下一个错误。
如何解决问题?

0 个答案:

没有答案