Definitelytyped: @types/leaflet Generic geojson feature properties

Created on 28 Sep 2017  路  7Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [ ] I tried using the @types/xxxx package and had problems.
  • [ ] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @alejo90 @iv-mexx @rbuckton @Timmmm @jramsay

Hi :smile:

I'd like to discuss a general proposal for leaflet types.

I work with leaflet and flowtype and wrote my own definitions. This project is not my latest version and far from being perfect but that's not the point.

The important thing is that I use a generic type for geojson properties and this leads to generic leaflet features. Say I create a new L.Polygon. It would be of type LeafletPolygon<PolygonProperties>. So when I want to have the 'classic' behavior it's just LeafletPolygon<Object>.

Since I started to work with typescript I really miss the type safety here.

What do you think?

Most helpful comment

Thank you so much! That's exactly what I was looking for!

All 7 comments

Hi, sorry for the delay. Can you provide a concrete example of what you're proposing? I haven't used geojson at all, so I confess I don't know what properties you would like to expose from the geometric types that would be related to geojson.

+1

It is also possible to add generics with defaults, so we don't have to change in other modules a lot:

class LeafletPolygon<P = Object> {
  // ...
}

If @alejo90 gives me a thump up on this, I could write an implementation for this...

@atd-schubert what I don't understand is what are we missing in the typings. Is it something dynamic and that's why it doesn't show up in the docs?

I see the problem now:

The type-definition of geojson (not the Leaflet one) is just using any as type for the property properties.

I thought there was something like myLeafletGeoJSON.properties, but there is just myLeafletGeoJSON.feature what is of type FeatureCollection<T>, I think, and the properties in a GeoJSON Feature is always of type any!

So @alejo90, you are right, I also can't see something missing here and it doesn't make any sense to enhance something here.

@kuuup-at-work maybe you can enhance geojson by using @yaga/generic-geojson, or something similar, to enhance your results with types for the properties, something like:

import { GenericGeoJSONFeature } from "@yaga/generic-geojson";
myLeafletGeoJSON.feature as GenericGeoJSONFeature<Polygon, IMonument>
 // ...

@atd-schubert thanks for the clarification. I now understand what this is about.
@kuuup-at-work maybe it makes sense to ask the geojson typings maintainers to add generic support to their typings first?

I will write the enhancements on the leaflet type-definition, according to the above mentioned changes in the GeoJSON one, these days...

Thank you so much! That's exactly what I was looking for!

Was this page helpful?
0 / 5 - 0 ratings