Javascript 带有D3.js的Typescript typecast
我正在使用所示的示例D3图。我在这里声明了以下数据对象:Javascript 带有D3.js的Typescript typecast,javascript,typescript,d3.js,Javascript,Typescript,D3.js,我正在使用所示的示例D3图。我在这里声明了以下数据对象: interface ID3Data { age: string, population: number } const data: ID3Data[] = [ { age: "<5", population: 2704659 }, { age: "5-13", population: 4499890 } ] 这将产生以下错误: TypeScript error: Argument of type 'ID3Data
interface ID3Data {
age: string,
population: number
}
const data: ID3Data[] = [
{ age: "<5", population: 2704659 },
{ age: "5-13", population: 4499890 }
]
这将产生以下错误:
TypeScript error: Argument of type 'ID3Data[]' is not assignable to parameter of type '(number | { valueOf(): number; })[]'.
Type 'ID3Data' is not assignable to type 'number | { valueOf(): number; }'.
Type 'ID3Data' is not assignable to type '{ valueOf(): number; }'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => number'.
Type 'Object' is not assignable to type 'number'. TS2345
因为很明显,
d3.pie().value()
使用了一种非常特定的输入数据类型,所以我在接收编译错误时做错了什么?因为D3的值函数是特定于它的库的。。。这是我可以在我的typescript代码中重写的东西吗?这是有问题的代码:
const pie = d3.pie()
.value(function(d: any) { return d.population; });
由于未指定要传递给生成器的数据类型,TypeScript编译器将使用以下命令:
这将使编译器改用以下选项:
导出函数饼图():饼图类型
const pie = d3.pie()
.value(function(d: any) { return d.population; });
export function pie(): Pie<any, number | { valueOf(): number }>;
const pie = d3.pie<ID3Data>()
.value(function(d) { return d.population; }); // Do not use any for the parameter!
export function pie<Datum>(): Pie<any, Datum>;