Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I import typedefs? #24

Open
avalanche1 opened this issue Feb 24, 2020 · 2 comments
Open

How can I import typedefs? #24

avalanche1 opened this issue Feb 24, 2020 · 2 comments
Assignees
Labels

Comments

@avalanche1
Copy link

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.

@KeithHenry KeithHenry self-assigned this Mar 10, 2020
@biggestcookie
Copy link

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants