我在 Y 轴格式上中继任务以仅显示自然数时遇到问题。在项目中,我使用 React awith Typescript 和 Nivo Rocks 库来创建图表(https://nivo.rocks/)。为了这个问题,我创建了沙箱:
https://codesandbox.io/s/scuqn?file=/src/App.tsx
我找到了设置格式的解决方案:
axisLeft={{
format: e => Math.floor(e) === e && e
}}
/>
但它仅适用于纯 js(无类型检查)。使用 TS 时出现类型错误(带有“e:number”):
No overload matches this call.
Overload 1 of 2, '(props: LineSvgProps | Readonly<LineSvgProps>): ResponsiveLine', gave the following error.
Type '(e: number) => number | undefined' is not assignable to type 'string | TickFormatter | undefined'.
Type '(e: number) => number | undefined' is not assignable to type 'TickFormatter'.
Types of parameters 'e' and 'value' are incompatible.
Type 'string | number | Date' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
Overload 2 of 2, '(props: LineSvgProps, context: any): ResponsiveLine', gave the following error.
Type '(e: number) => number | undefined' is not assignable to type 'string | TickFormatter | undefined'.
Type '(e: number) => number | undefined' is not assignable to type 'TickFormatter'.ts(2769)
我无法将类型与此解决方案匹配,并且我不能使用“任何”类型,我开始相信图书馆创建者并没有在他们的图书馆中预测到这个问题;)如果有人可以帮助我解决这些问题或提出其他解决方案我会很感激的:)