如何让 d3 形状构造函数在打字稿中正确 return 类型字符串?

How to get d3 shape constructors to return the type string properly in typescript?

我真的很困惑如何正确使用@types/d3。所有 return 类型确实令人困惑,甚至 console.logging 也无济于事。

本质上,当我调用 arc() 构造时,我得到一个错误:未提供 'd' 的参数。但是当我 console.log 它只是 return 正确的路径字符串而不是错误或无效值。

我似乎找不到在构造函数中将什么作为参数作为 d 值。另外,我看到的很多js代码都没有任何输入,typescript需要额外的值是什么具体原因?

const Test = () => {
      // ...........
     // causes error if mArc is not mArc: any
     const mArc = arc() // how to not force any as a type here
        .innerRadius(radius)
        .outerRadius(width + radius)
        .startAngle(Math.PI / 2)
        .endAngle((Math.PI * 3) / 2)
      
     console.log(mArc()); // returns a string...
     // but typescript throws an error, An argument for 'd' was not provided.
     return <path d={mArc()} />;
      
}

抱歉,我可能没有正确理解这个软件工程。当我查看 types.d 文件中的 d3-shape 时,它​​说没有找到 'Datum' 的定义,'d' 应该是

类型

正确答案:

const Test = () => {
     const mArc = arc<void, void>()
        .innerRadius(radius)
        .outerRadius(width + radius)
        .startAngle(Math.PI / 2)
        .endAngle((Math.PI * 3) / 2)
      
     console.log(mArc());
     return <path d={mArc() || ""} />;
      
}