【发布时间】:2025-12-30 05:50:12
【问题描述】:
我正在尝试显示一个饼图并且它确实在工作,即使我收到 TypeScript 编译错误:Argument of type '(d: any) => {}' is not assignable to parameter of type 'datum: Arc<number>, index: number, outerIndex: number) => number | string | boolean'. Type '{}' is not assignable to type 'number | string | boolean'. Type '{}' is not assignable to type 'boolean'.
过去 2 年我一直在使用 TypeScript,但上面的那行让我头晕目眩。我不知道发生了什么,我尝试了一些东西,但到目前为止没有任何效果。
这是实际绘制饼图的代码:
function drawChart(){
var width = 960,
height = 500,
radius = Math.min(width, height) / 2;
var colourValues = d3.scale.ordinal().range(d3.values(that.ColoursService.colours));
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);
var pie = d3.layout.pie()
.sort(null)
.value(function(d: any) {return d.quantity});
var svg = d3.select('.pie-chart').append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + width / 2 + ',' + height/2 + ')');
var g = svg.selectAll('.arc')
.data(pie(data))
.enter().append('g')
.attr('class', 'arc');
g.append('path')
.attr('d', <any>arc)
.attr('fill', function(d: any) {
return colourValues(d.data.category);
});
}
这是打字稿突出显示错误的地方,波浪线出现在“功能”字下方。我需要让 TypeScript 编译器通过,但我不知道怎么做。
【问题讨论】:
标签: javascript angularjs d3.js typescript