如何正确设置d3反应定稿应用程序中``路径元素in otthe d` d`的类型''
我正在尝试使用D3 符号()
方法绘制三角形,并将其分配给变量symboltri
。
import { symbol, symbolTriangle } from "d3"
const symbolTri = symbol().size(100).type(symbolTriangle)
export const Component = () => {
// ...
return (
<g>
<path d={symbolTri()} />
</g>
)
}
但是,在设置d
属性时,typescript错误type'string | null'不能分配给type'字符串|未定义'。
发生。当悬停在错误上时,它显示'd'值应该为D类型?:字符串|未定义;
。但是我无法正确设置它。任何帮助或建议都会有所帮助。
I'm trying to draw a triangle using the d3 symbol()
method and assigned it to a variable symbolTri
.
import { symbol, symbolTriangle } from "d3"
const symbolTri = symbol().size(100).type(symbolTriangle)
export const Component = () => {
// ...
return (
<g>
<path d={symbolTri()} />
</g>
)
}
But while setting the d
property a typescript error Type 'string | null' is not assignable to type 'string | undefined'.
occurred. And while hovering over the error it shows 'd' value should be of type d?: string | undefined;
. But I'm unable to set it properly. Any help, or suggestion would be really helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案,假设
symendtri()
的输出不能为null(在这种情况下似乎是合理的假设),就是使用 non-null断言操作员,它有效地告诉Typescript编译器,此变量永远不会为null
:The easiest solution, assuming that the output of
symbolTri()
cannot be null (which seems a reasonable assumption in this case), is to use the non-null assertion operator, which effectively tells the TypeScript compiler that this variable will never benull
: