Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type '(err: any, data: any) => void' has no properties in common with type 'RequestInit'

I'm following this example https://www.mapbox.com/mapbox-gl-js/example/timeline-animation/ to create a time based visualisation. I'm using this version "d3": "^5.4.0" The code is:

d3.json('http://127.0.0.1:5000', function (err, data) {
        if (err) throw err;

        // Create a month property value based on time
        // used to filter against.
        data.features = data.features.map(function (d) {
          d.properties.month = new Date(d.properties.time).getMonth();
          return d;
        });

        map.addSource('visits', {
          'type': 'geojson',
          'data': data
        });

        map.addLayer({
          'id': 'visits-circles',
          'type': 'circle',
          'source': 'visits',
          'paint': {
            'circle-color': [
              'interpolate',
              ['linear'],
              ['get', 'name'],
              6, '#FCA107',
              8, '#7F3121'
            ],
            'circle-opacity': 0.75,
            'circle-radius': [
              'interpolate',
              ['linear'],
              ['get', 'name'],
              6, 20,
              8, 40
            ]
          }
        });

        map.addLayer({
          'id': 'visits-labels',
          'type': 'symbol',
          'source': 'visits',
          'layout': {
            'text-field': ['concat', ['to-string', ['get', 'name']], 'm'],
            'text-font': ['Open Sans Bold', 'Arial Unicode MS Bold'],
            'text-size': 12
          },
          'paint': {
            'text-color': 'rgba(0,0,0,0.5)'
          }
        });

        // Set filter to first month of the year
        // 0 = January
        filterBy(0);

        document.getElementById('slider').addEventListener('input', function (e) {
          var month = parseInt(e.target.value, 10);
          filterBy(month);
        });

I'm doing exactly the same thing with the URL to my data but I'm getting some error messages

error TS2559: Type '(err: any, data: any) => void' has no properties in common with type 'RequestInit' error TS2339: Property 'value' does not exist on type 'EventTarget'.

Does anybody have any idea about how to solve it?

like image 569
HoudaF Avatar asked Jun 05 '18 07:06

HoudaF


2 Answers

The type information for d3 suggests a promise based interface - perhaps older versions used callbacks.

Your code follows a callback pattern:

d3.json('http://127.0.0.1:5000', function (err, data) {
    // Handle err

    // Use data
});

Here is the promise version:

d3.json('http://127.0.0.1:5000')
    .then((data) => {
        // Use data
    })
    .catch((err) => {
        // Handle err
    });

Typed Response

The data that you get back can be typed. Pass a type argument to the json method to tell it what kind of data you will get back. For example:

interface ResponseData {
  features: any[];
}

d3.json<ResponseData>('http://127.0.0.1:5000')
.then((data) => {
    // Use data
})
.catch((err) => {
    // Handle err
});
like image 156
Fenton Avatar answered Sep 23 '22 19:09

Fenton


I had the same problem. A workaround is use

(d3 as any)

as in:

(d3 as any).json(options.data, (error, d) => {
like image 36
techguy2000 Avatar answered Sep 22 '22 19:09

techguy2000