Turf: Turf typings don't play nicely with mapbox-gl typings

Created on 5 Mar 2018  路  10Comments  路  Source: Turfjs/turf

I've been working with mapbox-gl in Angular 5, and it requires using types/geojson (I am currently on v0.44.0 for the mapbox-gl typings). When I try to use your latest versions of turf, it looks like you have your own internal geojson library that you are using (it looks like since v5.1.0). So when I try to use turf helper functions such as lineString() and polygon(), it doesn't work well with mapbox-gl's typings as it sees the geojson objects as different types. Any suggestions?

typescript

Most helpful comment

Any word on a fix for this?

All 10 comments

Thanks for the report @bryanjjohnson .

@DenisCarriere is probably the best person to respond as he is our Typescript guru.

My understanding was that both Turf and Mapbox-gl typings both rely on @types/geojson. So I'm not sure what might be causing the incompatability...

Over to Denis :)

Any suggestions?

The latest release @types/geojson broke TurfJS, that's the reason behind including the internal GeoJSON typing.

We can "one day" switch over back to the @types/geojson typings, however a lot of code will have to be refactored to match the minor differences between both typings.

@bryanjjohnson Can you show what type of code is not working? There might be a few comprises we could do to patch the situation.

What is it about @types/geojson that broke things @DenisCarriere ?

It was this commit/PR:
https://github.com/DefinitelyTyped/DefinitelyTyped/commit/89667eb5cbee013f697ef1adef6582caa62385f3#diff-4f5aa92a196385006e0183c771a95f12

It just made things "more strict" (which is good... however it a few TurfJS typescript definitions).

Also having our entire build break because some "dependency" changed seems difficult to manage.

We can reviste adding @types/geojson as a dependency for @turf/helpers, it shouldn't be too hard to implement, we might just need to refactor a few modules to patch it.

Yeah I think it's worth looking at - if you'd like TurfJS to be a typescript library then I would've thought it would make sense that we use the standard type library that other geo libs are likely to reference (mapboxgl and others).

I guess this is one of the downsides of types, you actually have to manage and implement them correctly!

I'm having the same issue. Ideally, I could use a turf library function and just pass it off to mapbox-gl, but that's not the case.

@DenisCarriere Is there any work you need me to do, or would this cloud a v6.0 release schedule?

@andest01 you could help convert some of the current libraries using a strict type configuration.

$ tsc --init
$ tslint --init

Just follow some of the early modules like turf-angle

I'm going to be working on this in the afternoon

@DenisCarriere great! I tried to get this up and running and I ran into a few very basic snags after reading the contribution guidelines. Since this is my first effort in contributing to turf, it would help if i could work closely with someone more experienced, over slack or something like that.

Let me know how I can proceed. :)

$ cd packages/turf-angle
$ npm install
$ npm run prepare

node_modules/@turf/rhumb-bearing/index.ts(49,32): error TS7006: Parameter 'from' implicitly has an 'any' type.
node_modules/@turf/rhumb-bearing/index.ts(49,38): error TS7006: Parameter 'to' implicitly has an 'any' type.

Obviously, it's trying to pull from node_modules, and at this point I bailed.

Any word on a fix for this?

I'm running into problems as well. For example when adding a source to mapbox-gl Map instance, I pass it a geojson object created by turf which I typed as following:

const source: GeojJSONObject & { data: FeatureCollection } = {
  type: 'geojson',
  data

When trying to use it with Mapbox like this: map.addSource(id, source) it will unfortunately lead to type error:

[tsserver 2345] [E] Argument of type 'GeoJSONObject & { data: FeatureCollection<Geometry | GeometryCollection, Properties>; }' is not assignable to parameter of type 'AnySourceData'.
  Type 'GeoJSONObject & { data: FeatureCollection<Geometry | GeometryCollection, Properties>; }' is not assignable to type 'GeoJSONSourceRaw'.
    Types of property 'type' are incompatible.
      Type 'string' is not assignable to type '"geojson"'.

This library is very useful and seems like it should play nicely with Mapbox, however for now I have to basically resort to ts-ignore when passing Turf objects to map objects.

When reading the other issue there does not seem to be any activity there. I understand that you might want to avoid TS in the codebase but having types that play nicely with other geo libs would be very much preferrable.

Was this page helpful?
0 / 5 - 0 ratings