zmbq
zmbq

Reputation: 39039

Overriding type defintions in TypeScript

I'm trying to develop a Chrome Extension in TypeScript. I have installed @types/chrome and everything seemed in order.

Now I want to use chrome-extension-async which provides a Promisified version of the chrome extension objects, allowing me to async/await.

I can't seem to figure out how to enable this extension, though. The documentation just says I should add a <script> pointing to the extension's js file. My Chrome extension has no HTML pages, so there's no <script> tag.

UPDATE: Following @basarat's advice I've stopped using @types/chrome and added chrome-extension-async.d.ts to my tsconfig. Typescript does not complain now, but Chrome does. Chrome sees the old chrome namespace, not the overridden one, so when it sees a chrome.storage.sync.get call with no callback, it complains.

If I import the extension, with something like

import * as chromeAsync from "chrome-extension-async";

the import works, but I can't await chrome.storage.sync.get('field) because TypeScript complains (quite rightly) that I'm not passing a callback.

I'm really stuck here.

Upvotes: 1

Views: 396

Answers (2)

evans
evans

Reputation: 59

Function is also an object in TypeScript and you and make any object of type 'any'. Doing this it will accept whatever parameter you pass to this function.

try: < any>(chrome.storage.sync.get)()

Upvotes: 0

basarat
basarat

Reputation: 276265

the import works, but I can't await chrome.storage.sync.get('field) because TypeScript complains (quite rightly) that I'm not passing a callback.

Dont use @types/chrome and instead use https://github.com/KeithHenry/chromeExtensionAsync/blob/master/chrome-extension-async.d.ts

More

From the docs enter image description here

Upvotes: 1

Related Questions