question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incompatible between browser.tabs.query and browser.tabs.update

See original GitHub issue

Hi! I have this code:

import { browser } from 'webextension-polyfill-ts';

async function fn() {
    const tabs = await browser.tabs.query({});
    browser.tabs.update(tabs[0].id, {active: true});
}

And ts shows error on tabs[0].id:

TS2345: Argument of type ‘number | undefined’ is not assignable to parameter of type ‘number’.   Type ‘undefined’ is not assignable to type ‘number’.

is it intended or a bug?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Lusitocommented, Oct 19, 2018

Sure. I’m sure you know, that in TypeScript, optional parameters may not be followed by non-optional parameters:

function a(b: string, c?: string) {} // Valid
function a(b?: string, c: string) {} // Invalid

The previous approach to solve this was like this: If a function has non-optional parameters following optional parameters, do:

  1. Get the first non-optional parameter.
  2. remove the optional flag from the first non-optional parameter, and generate the code for this modified function (a recursive call, i.e. it might again do this trick)
  3. Remove the parameter altogether and generate the code for this modified function (again recursively)

Example:

function a(b?: string, c: string) {} // Source
function a(b: string, c: string) {} // Generated
function a(c: string) {} // Generated

The new approach does the same thing, but instead of removing the optional flag, it sets an internal overloadFlag. If this is set, then the parameter doesn’t match when looking for the first optional parameter that is followed by a non-optional parameter. That way, the optional flag stays in the parameter and I use it later to add the “| undefined” instead of the “?”.

Example:

function a(b?: string, c: string) {} // Source
function a(b: string | undefined, c: string) {} // Generated
function a(c: string) {} // Generated

The commit for this was: https://github.com/Lusito/webextension-polyfill-ts/commit/cf4f93db834e49ee485453769675f4282b66b1c5

1reaction
Lusitocommented, Oct 18, 2018

@vitalets I’ve released a new version, which fixes the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tabs.update() - Mozilla - MDN Web Docs
This is an asynchronous function that returns a Promise . Syntax. let updating = browser.tabs.update( ...
Read more >
Tabs cannot be queried right now (user may be dragging a tab)
I found that the real reason is: when you click the mouse to switch tabs, when chrome.tabs.
Read more >
How tabs should work - Remy Sharp
I used to be from the school of using the hash to show the right tab, but I've recently been exploring whether the...
Read more >
Q. Why can't I have Gladly running on multiple browser tabs?
For example, moving between two Gladly sessions on separate browser tabs may cause inconsistent time tracking.
Read more >
Supported APIs for Microsoft Edge extensions
When building extensions for the Microsoft Edge browser, ... sessions, Query and restore tabs and windows from a browsing session.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found