Home > OS >  'key' is declared but its value is never read when set google chrome extension v3 local st
'key' is declared but its value is never read when set google chrome extension v3 local st

Time:02-16

I define a function with typescript like this:

setLocalStorage: async (key: string, value: string): Promise<string> => {
    return new Promise((resolve, reject) => {
      chrome.storage.local.set({
        key: value
      }, function () {
        resolve("");
      });
    });
  },

but the Visual Studio Code shows error:

'key' is declared but its value is never read.ts(6133)

the key was obviously used in the chrome storage set function, why did this happen? what should I do to fix it?

CodePudding user response:

In order to use the key value not the "key" as property name, you should modify the code like this(note the square brackets):

setLocalStorage: async (key: string, value: string): Promise<string> => {
    return new Promise((resolve, reject) => {
      chrome.storage.local.set({
        [key]: value
      }, function () {
        resolve("");
      });
    });
  },
  • Related