Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'

I am trying to create a chrome extension in typescript.

I have the following code, where I try to send a message to contentscript from the background script.

// background script 
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
   chrome.tabs.sendMessage(tabs[0].id, {action: "open_dialog_box"}, function(response) {
      console.log(response);
   });  
});

but typescript is giving the following error

ERROR in /Users/shobi/Projects/Chrome Extensions/src/js/background.ts(8,37)
      TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.

I tried converting the id to an integer using parseInt(), but still its not preventing the error.

like image 704
Shobi Avatar asked Oct 16 '18 14:10

Shobi


People also ask

Is not assignable to parameter of type '() =>?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.

Is not assignable to parameter of type undefined?

The "Type 'undefined' is not assignable to type" error occurs when a possibly undefined value is assigned to something that expects a different type. To solve the error, use the non-null assertion operator or a type guard to verify the value is of the specific type before the assignment.

Is not assignable to parameter of type String TypeScript?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.

Is not assignable to type null?

The "Type 'string | null' is not assignable to type string" error occurs when a possibly null value is assigned to something that expects a string . To solve the error, use a non-null assertion or a type guard to verify the value is a string before the assignment.


2 Answers

Chrome's tab .id is possibly undefined (because it's defined as optional in the type definitions), which means when using strictNullChecks you have to either use a non-null assertion:

tabs[0].id!

Or write code to ensure that id is not null:

if (tabs[0].id) { // send message
like image 61
Aaron Beall Avatar answered Nov 15 '22 11:11

Aaron Beall


I'm guessing, but maybe typescript thinks that your tabs array could be empty (and thus not have an item at index [0]) - which is why it's type is inferred as number or undefined?

If this is the case, you should check that tabs[0] exists before trying to access the id property on it.

like image 20
Filip Wojciechowski Avatar answered Nov 15 '22 10:11

Filip Wojciechowski