Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 typescript d3 type issue: Property 'x' does not exist on type '[number, number]'

I am generating a line chart with d3. It works, but typescript code complain about a property not exists in vs.

Property 'x' does not exist on type '[number, number]'

enter image description here

Looking at the error. It seems like the data point expected is an array with two numbers.

But I am passing in an object. D3 should support both I think.

Does anyone know how to get rid of this error without changing my data?

like image 388
techguy2000 Avatar asked Jan 28 '17 00:01

techguy2000


2 Answers

Here is the solution. I need to use generics:

enter image description here

enter image description here

like image 128
techguy2000 Avatar answered Sep 19 '22 11:09

techguy2000


When data is passed as a variable, defining that data as type any should appease TypeScript, e.g.:

// Append a 'text' SVGElement with data-driven text() to each 'g' SVGElement 
d3.selectAll('g')
  .each(function(d: any) {
    d3.select(this)
      .append('text')
      .text(d.mytext);
  });

like image 35
jared jessup Avatar answered Sep 20 '22 11:09

jared jessup