Angular 2 typescript Problem of type d3: property 'x' does not exist in type '[number, number]'

I am creating a line graph with d3. This works, but the typescript complains about the property does not exist in comparison

Property 'x' does not exist for type '[number, number] ”

enter image description here

Looking for a mistake. The expected data point seems to be an array with two numbers.

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

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

+11
source share
5 answers

Here is the solution. I need to use generics:

enter image description here

enter image description here

+21
source

, TypeScript. , . :

let line = d3.line()
  .x(function (d) {
    return x(d['date']);
  })
  .y(function (d) {
    return y(d['temp']);
  });
+5

typescript.

, d x.

 return this.xScale(d?.x);
 return this.xScale(d?.y);

d , ["x_value","y_value"] .

return this.xScale(d[0]);
return this.yScale(d[1]);

,

+1

- Typescript (?).

function buildName(firstName: string, lastName?: string) {
    if (lastName)
        return firenter code herestName + " " + lastName;
    else
        return firstName;
}

let result1 = buildName("Bob");                  // works correctly now
let result2 = buildName("Bob", "Adams", "Sr."); // error, too many parameters
let result3 = buildName("Bob", "Adams");    // ah, just right

https://www.typescriptlang.org/docs/handbook/functions.html

+1

, any TypeScript, :

// 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);
  });

0

Source: https://habr.com/ru/post/1668068/


All Articles