KeithHenry / chromeExtensionAsync

Promise wrapper for the Chrome extension API so that it can be used with async/await rather than callbacks
MIT License
228 stars 32 forks source link

How can I import typedefs? #24

Open avalanche1 opened 4 years ago

avalanche1 commented 4 years ago

Namely, I need the def of async version of chrome.tabs.getCurrent(). When I try to import chrome from "chrome-extension-async/chrome-extension-async"; TS says node_modules/chrome-extension-async is not a module.

biggestcookie commented 4 years ago

Was just figuring this out yesterday. This package is a wrapper written in JavaScript that provides a TypeScript definition file. In order to import definition files, you either import it into your code like this:

import "chrome-extension-async";

or by adding "chrome-extension-async" to your tsconfig.json, under compilerOptions.types, like so:

{
  "compilerOptions": {
    "types": [
      "chrome-extension-async"
    ],
    // other options
  }
}

and then you will have access to the chrome namespace and your code and types using chrome will appear correctly. Additionally if you only need access to one or a few APIs under chrome, you can use a namespace alias to alias that API to a shorter name.

import "chrome-extension-async";
chrome.storage.local.clear(); // the regular way

import storage = chrome.storage;
storage.local.clear(); // Now chrome.storage is accessible under storage

@KeithHenry Many popular packages are often imported as modules and not declarations and the TypeScript usage may not be obvious to some, so this may be worth putting in the README.

KeithHenry commented 4 years ago

@biggestcookie good idea! PRs welcome :) Otherwise I'll get round to it when I update the docs for the next release.