microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.14k stars 12.5k forks source link

ServiceWorkerRegistration.Update() should return Promise<ServiceWorkerRegistration> #31502

Open ahardin13 opened 5 years ago

ahardin13 commented 5 years ago

TypeScript Version: 3.4.3

Search Terms: ServiceWorkerRegistration Update

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
let isUpdating: boolean;
navigator.serviceWorker.getRegistration().then(sw => {
    sw.update().then(result => {
        if (result.installing || result.waiting) {
            isUpdating = true;
        }
    });
});

Expected behavior: result is of type ServiceWorkerRegistration and result.installing and result.waiting are valid. https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/update

Actual behavior: result is of type void and tsc gives errors error TS2339: Property 'installing' does not exist on type 'void'. error TS2339: Property 'waiting' does not exist on type 'void'.

[Playground Link](https://www.typescriptlang.org/play/#src=let%20isUpdating%3A%20boolean%3B%0D%0Anavigator.serviceWorker.getRegistration().then(sw%20%3D%3E%20%7B%0D%0A%20%20%20%20sw.update().then(result%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20if%20(result.installing%20%7C%7C%20result.waiting)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20isUpdating%20%3D%20true%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D)%3B%0D%0A%7D)%3B%0D%0A)

Related Issues: https://github.com/microsoft/TypeScript/issues/17590

ahardin13 commented 5 years ago

It looks to me like this definition is what needs to be updated. https://github.com/microsoft/TypeScript/blob/00bf32ca3967b07e8663d0cd2b3e2bbf572da88b/lib/lib.webworker.d.ts#L2721

ahardin13 commented 5 years ago

After digging through how the changes would be made, I found the typescript definition matches the w3c spec. https://w3c.github.io/ServiceWorker/#serviceworkerregistration

Any guidance on if I should follow-up on that spec or stick with the current definition and not use Chrome's implementation?

MartinJohns commented 5 years ago

On MDN they write for Chrome:

Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.

olback commented 5 years ago

On MDN they write for Chrome:

Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.

This seems to have changed. Running the update method in Chromium 77, I get a ServiceWorkerRegistration object back.

Edit: Tried it in Firefox 69 as well, same result as in Chromium 77.

dlech commented 3 years ago

It looks like this need to be fixed here: https://github.com/microsoft/TypeScript-DOM-lib-generator