Leaflet has a prosperous ecosystem with hundreds of plugins. Most of them are not typed for TypeScript, however, with a minimal setup, you are able to use these plugins in your TypeScript mapping project.
Since TypeScript is definitely typed, a simple workaround is to provide a minimal type declaration file that can expose the plugin functions in the L
namespace.
For example, I would like to display a large GeoJSON file at the browser using the vector tile technique. I can do it in JavaScript with the following code:
1 | // Get the GeoJSON somewhere |
The vector tile plugin Leaflet.VectorGrid isn’t officially typed and you are not able to directly use this plugin because the compiler doesn’t know its existence. So we just need to declare it with a leaflet.vectorgrid.d.ts
file
1 | // in the global namesapce "L" |
Then we are able to use the function in TypeScript
1 | /// <reference path="leaflet.vectorgrid.d.ts"/> |
Here we go!
If we want to use more, we could continue to populate the type declaration file and maybe contribute it to the community when it becomes more complete.
For a fully functional exampe, see angular2-leaflet-starter.