(我刚开始学习打字稿,所以我敢打赌这对于熟悉打字稿的人来说是微不足道的)
我有一个带有以下代码的图表(为简洁起见,我已删节):
const pieOptions = {
plugins: {
datalabels: {
display: true,
color: "white",
formatter: function (value, context) {
return `${value}%`;
},
font: {
size: "15",
weight: "bold"
}
}
}
};
export const HaveAdhdPie: React.FC<Props> = () => {
return (
<div>
<Pie type="pie" data={data} options={pieOptions} />
</div>
);
};
Typescript 将options
prop 标记为错误:
错误消息的一部分:
Types of property 'weight' are incompatible.
Type 'string' is not assignable to type 'number | "normal" | "bold" | "bolder" | "lighter"'.
index.d.ts(26, 3): The expected type comes from property 'options' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Pie> & Readonly<ChartComponentProps> & Readonly<...>'
所以我转到 index.d.ts 文件,并找到options.plugins
定义类型的位置:
// NOTE: declare plugin options as interface instead of inline '{ [plugin: string]: any }'
// to allow module augmentation in case some plugins want to strictly type their options.
interface ChartPluginsOptions {
[pluginId: string]: any;
}
据我了解,我被困住了。我不认为我可以更改type.d.ts
文件,我不明白上面代码中的注释在指导我做什么。
谁能建议打字稿接受我的代码需要什么?