Mapbox-gl typing won't allow accessToken assignment

Viewed 7752

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 accessToken to use the library, my TypeScript compiler throws this error: TS2540: Cannot assign to 'accessToken' because it is a constant or a read-only property.

So I pulled up the .d.ts file and the variable in question looks extremely assignable (seen here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mapbox-gl/index.d.ts):

declare namespace mapboxgl {
   let accessToken: string;
   ...

This is my code:

import * as mapbox from 'mapbox-gl';
mapbox.accessToken = 'token';

Versions!

"@types/mapbox-gl": "^0.35.0",
"typescript": "^2.3.4",
"mapbox-gl": "^0.37.0",

TypeScript hackery says that I can cast mapbox to any and it will work, but I'm very curious what about the typing is going wrong here.

3 Answers

You can also use this format:

(mapboxgl as typeof mapboxgl).accessToken = ...
Related