Unable to access global L namespace after installi

2019-07-27 02:56发布

问题:

I am struggling to get a reference to an exported namespace for the leaflet type definitions in my TypeScript project.

After installing the types using npm install --save @types/leaflet my TS compiler (I am using VS Code) is able to find some of the exported classes and interfaces in the type definition file, e.g. Transformation, Class, but I am unable to access any of the namespaces exported.

I need to be able to reference the L global so I can make calls to the leaflet API, but the compiler is not picking this up for me.

I did try using an import statement in my TS file I am trying to use leaflet in, e.g.

import * as L from 'leaflet'

But again, I can still only access the classes\interfaces this time on my imported alias, e.g. L.Transformation

I cannot initialise a new map, e.g by using L.map(...) since L is not accessible for me - map is an exported function in the type definition file

So I am unsure how to reference and use the L global for leaflet in my project.

Can anyone assist please?

Thanks

回答1:

Try using an older version of @types/leaflet. I have the same issue and solved it by using the 1.0.69 version. npm install --save @types/leaflet@1.0.69

Using anything above that (1.2+) seems to lead to this error.