Search code examples
typescriptgoogle-chrome-extensionvoid

Using properties from function returned object in TypeScript


I have an object stored in browser local storage that looks something like this:

{
  url: 'https://example.com',
  appId: 'abcd',
  appEnabled: true
}

I'd like to retrieve it from Chrome local storage and base some logic on values contained within that object. I'm a little stuck with setting the values from the retrieved object and keep getting Type 'void' is not assignable to type '{url: string, appId: string, appEnabled: boolean} error. How do I go about setting properties within options with values returned from chrome local storage API?

This is what I tried so far:

var options: {
  url: string,
  appId: string,
  appEnabled: boolean
}
options = chrome.storage.local.get('options', function (data) { return data.options });

if(options.appEnabled){
  addOverlay(options.appId);
}

Solution

  • Please try this.

    chrome.storage.local.get('options', function (result) {
      const options = result['options'];
    
      if(options.appEnabled){
        addOverlay(options.appId);
      }
    });