对象文字只能指定已知属性,并且名称'类型不存在,但类型存在
奇怪的问题,我会收到上述错误,我无法弄清楚为什么,这是一个屏幕截图显示所有内容:
这是我的代码:
const createSeries = (report: ReportEntry[], key: string) => {
const reportEntryValues: any = [];
let itemColor: string;
let series: ApexAxisChartSeries;
report.forEach((item: ReportEntry) => {
reportEntryValues.push(item[key as keyof typeof item]);
});
switch (key) {
case "approved":
itemColor = Colors.Green;
break;
case "denied":
itemColor = Colors.Red;
break;
default:
itemColor = Colors.Blue;
}
series = {
name: key,
type: "bar",
color: itemColor,
data: reportEntryValues,
};
return series
};
任何帮助都非常感谢,我一直在谷歌搜索为什么会发生这种情况,该代码工作正常,但是Typescript仍在向我尖叫。
提前致谢!
EDIT: 游乐场链接
strange issue, I get the above mentioned error and I can't figure out why, here's a screenshot showing everything:
Here's my code:
const createSeries = (report: ReportEntry[], key: string) => {
const reportEntryValues: any = [];
let itemColor: string;
let series: ApexAxisChartSeries;
report.forEach((item: ReportEntry) => {
reportEntryValues.push(item[key as keyof typeof item]);
});
switch (key) {
case "approved":
itemColor = Colors.Green;
break;
case "denied":
itemColor = Colors.Red;
break;
default:
itemColor = Colors.Blue;
}
series = {
name: key,
type: "bar",
color: itemColor,
data: reportEntryValues,
};
return series
};
any help is hugely appreciated, I've been googling why this happens, the code works just fine but typescript is still screaming at me.
Thanks in advance!
EDIT: playground link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
apexaxischartseries
是一个数组类型。因此,您需要更改apexaxischartseries
为对象或返回数组。Playground with solutions
The problem is that
ApexAxisChartSeries
is an Array type. So you ether need to change theApexAxisChartSeries
to be an Object or return an array.Playground with solutions