Mapbox-gl typing won't allow accessToken assignment

前端 未结 3 2520
日久生厌
日久生厌 2021-02-20 00:54

I\'m using the mapbox-gl library with TypeScript, and I\'ve installed its community sourced type definitions with @types/mapbox-gl. When I try to import and set an

3条回答
  •  忘掉有多难
    2021-02-20 01:41

    Here's a temporary workaround I've been using:

    Object.getOwnPropertyDescriptor(mapboxgl, "accessToken").set('YOUR_TOKEN');
    

    Explanation

    Since the object was redefined to use a custom setter which places the token inside an internal closure - we can call the setter function directly as shown in the example.

    Diving a little deeper, we can see that es6 modules are constants by definition: https://github.com/Microsoft/TypeScript/issues/6751#issuecomment-177114001

    we can then do something like: (mapboxgl as any).accessToken = ... which will work.

提交回复
热议问题