Home > database >  How to import vanilla JavaScript objects in TypeScript
How to import vanilla JavaScript objects in TypeScript

Time:06-11

I have a traditional JavaScript file, say abc.js that does something like this:

// abc.js
if (typeof ABC !== "object") {
    ABC = {};
}
(function () {
   // ... construct the ABC object
})

This is a 3rd-party dependency that I cannot touch.

Then, in my TypeScript file, say main.ts, I want to include the ABC object defined in abc.js so that the TypeScript checker can understand it.

Ideally I would like to end up with a single .js file after the compilation. What's the correct way of doing it?

CodePudding user response:

You can access to the JavaScript Object through window For example: import "spotlight" window.Spotlight.show(...) How to import Vanilla JS ES6 modules in TypeScript?

CodePudding user response:

You might need to tweak tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false,
  }
}

and import in main.ts:

import * as ABC from './abc';
  • Related