Select to view content in your preferred language

TypeScript errors when using $arcgis.import

293
2
Jump to solution
04-03-2025 09:40 AM
JeffJacobson
Frequent Contributor

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");
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Esri Frequent Contributor

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 };
}

 

View solution in original post

2 Replies
ReneRubalcava
Esri Frequent Contributor

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 };
}

 

JeffJacobson
Frequent Contributor

@ReneRubalcava:

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.)