gpt4 book ai didi

reactjs - 用 typescript 对 js 作出 react

转载 作者:行者123 更新时间:2023-12-05 01:28:30 28 4
gpt4 key购买 nike

我将 react-plotly.js 与 typescript 一起使用,我从中得到以下错误:

No overload matches this call.
Overload 1 of 2, '(props: PlotParams | Readonly<PlotParams>): Plot', gave the following error.
Type '({ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; } | { type: string; x: number[]; y: number[]; mode?: undefined; marker?: undefined; })[]' is not assignable to type 'Data[]'.
Type '{ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; } | { type: string; x: number[]; y: number[]; mode?: undefined; marker?: undefined; }' is not assignable to type 'Data'.
Type '{ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; }' is not assignable to type 'Data'.
Type '{ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; }' is not assignable to type 'Partial<BoxPlotData>'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"box" | undefined'.
Overload 2 of 2, '(props: PlotParams, context: any): Plot', gave the following error.
Type '({ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; } | { type: string; x: number[]; y: number[]; mode?: undefined; marker?: undefined; })[]' is not assignable to type 'Data[]'. TS2769

20 | };
21 |
> 22 | const Dashboard = () => <div><Plot data={data} layout={layout} /></div>;
| ^
23 |
24 | export default Dashboard;
25 |

我用它发送到以下数据,但它似乎不起作用。

const data = [{
type: 'scatter',
mode: 'lines+points',
x: [1, 2, 3],
y: [2, 6, 3],
marker: { color: 'red' },
}, {
type: 'bar',
x: [1, 2, 3],
y: [2, 5, 3],
}];

const layout = {
width: 640,
height: 480,
title: 'A Fancy Plot',
};

const Dashboard = () => <div><Plot data={data} layout={layout} /></div>;

这与我发送的数据有关吗?我安装了 react-plotly 的类型,因此出现了这个错误。

最佳答案

我在尝试将 react-plotly.js 与 TypeScript 一起使用时遇到了类似的问题。

这里你传递的是“lines+points”,这不是一个有效的类型,但即使你传递了一个正确的类型,仍然面临这个问题。

我就是这样解决的!

需要的包:

npm i plotly.js react-plotly.js

开发依赖:

npm i --save-dev @types/plotly.js @types/react-plotly.js

Chart.tsx 文件示例:

import React from "react";
import Plotly from "plotly.js";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);

export default function LineChart() {
const data = [
{
x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
mode: "lines",
},
];
const layout = { title: "Chart Title" };

return <Plot data={data} layout={layout} />;
}

关于reactjs - 用 typescript 对 js 作出 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68518749/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com