Using chrome extension apis in typescript

2020-05-13 03:20发布

I'm building a chrome extension written in TypeScript. I'm using WebStorm and I added the chrome-DefiniteltyTyped library in my project.

However, when I write this in my typescript code : chrome.extension.getURL I got an error : cannot find name 'chrome'.

Because of this, my javascript file is not generated and I cannot use it in my extension.

Do you guys have any solution?

2条回答
forever°为你锁心
2楼-- · 2020-05-13 03:56

That should work fine : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/chrome/index.d.ts

TIP: make sure you have a reference tag added:

/// <reference path="pathTo/chrome.d.ts"/>

查看更多
Bombasti
3楼-- · 2020-05-13 04:03

As of typescript 2 (or 2.x, not sure), you should import the chrome types from @types.

in package.json:

"devDependencies": {
    ...
    "@types/chrome": "0.0.35", // or just npm install --save-dev @types/chrome

And in tsconfig:

    "types": [
        //(various types, e.g. jquery, core-js),
        "chrome"
    ]
查看更多
登录 后发表回答