When I try to use $arcgis.import to load modules via CDN, TypeScript throws an error, saying there is no global variable named $argis.
error TS2339: Property '$arcgis' does not exist on type 'Window & typeof globalThis'.
How do I get TypeScript to recognize $arcgis?
I've figured out that $arcgis is defined in "@arcgis/core-adapter". I can get Visual Studio Code to stop complaining if I add "@arcgis/core-adapter" to package.json, but tsc still throws the error.
// This gets Visual Studio Code to stop complaining, but not tsc.
const FeatureLayer = await window.$arcgis.import<typeof __esri.FeatureLayer>("@arcgis/core/layers/FeatureLayer.js");
Solved! Go to Solution.
You can declare this on the global type in your app
export declare global {
// maybe this
// var $arcgis: { import<T>?: (modules: string[]) => Promise<T> | null };
var $arcgis: { import?: (modules: string[]) => Promise<unknown> | null };
}
You can declare this on the global type in your app
export declare global {
// maybe this
// var $arcgis: { import<T>?: (modules: string[]) => Promise<T> | null };
var $arcgis: { import?: (modules: string[]) => Promise<unknown> | null };
}
I have created an improved type definition for $arcgis.import and put it on GitHub. It is Unlicensed, so anyone can use it and incorporate it into their own code without asking for permission.
I used ChatGPT to help generate a script to dump a list of mappings, so developers using $arcgis.import no longer need to specify the type parameter.
I haven't yet packaged it and put it in a repository (e.g., NPM), but I think users can directly add the GitHub repo as a package to their project. (I haven't tested this. If that doesn't work, they can just copy the d.ts file into their own project.)