使用react flow遇到了奇怪的问题,好在GitHub issues有解决方案

104 阅读1分钟

image.png

最近我在使用xyflow库时遇到了一个TypeScript错误,错误信息如下:

Type '"dots"' is not assignable to type 'BackgroundVariant | undefined'.ts(2322)

这个错误是因为我尝试将字符串 'dots' 赋给 variant 属性,但是这个属性期望的是 BackgroundVariant 枚举中的一个值或者是 undefined

经过一番搜索和尝试,我找到了解决方案。如果您也遇到了同样的问题,可以尝试以下方法:

确保您赋给 variant 的值是 BackgroundVariant 枚举中定义的。例如,如果枚举中有一个名为 Dots 的成员,那么您应该这样写:

<Background variant={BackgroundVariant.Dots} />

希望这个解决方案对你有帮助!


参考链接:Type '"dots"' is not assignable to type 'BackgroundVariant | undefined'. · Issue #3655 · xyflow/xyflow (github.com)