TypeScript ServiceWorkerRegistration.Update() 应该返回 Promise< ServiceWorkerRegistration>

xqnpmsa8  于 3个月前  发布在  TypeScript
关注(0)|答案(5)|浏览(29)

TypeScript版本: 3.4.3
搜索词:

ServiceWorkerRegistration Update

代码

// 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;
        }
    });
});

预期行为:

resultServiceWorkerRegistration 类型,并且 result.installingresult.waiting 是有效的。
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/update

实际行为:

result 是 void 类型,tsc 报错
error TS2339: Property 'installing' does not exist on type 'void'.
error TS2339: Property 'waiting' does not exist on type 'void'.

Playground链接
相关问题:

#17590

oug3syen

oug3syen1#

在我看来,这个定义需要更新。
TypeScript/lib/lib.webworker.d.ts
第2721行 00bf32c
| | update(): Promise; |

cngwdvgl

cngwdvgl2#

在深入研究这些变化将如何进行后,我发现TypeScript定义与W3C规范相匹配。https://w3c.github.io/ServiceWorker/#serviceworkerregistration
关于是否应该跟进该规范或坚持使用当前定义而不使用Chrome的实现,有任何指导吗?

eyh26e7m

eyh26e7m3#

在MDN上,他们为Chrome编写了以下内容:
从Chrome 46开始,update()函数返回一个promise。如果操作成功完成或者没有更新,该promise将解析为'undefined',如果更新失败,则会拒绝。即使新的worker运行了,但安装失败,该promise仍然会解析。以前,它会引发一个异常。

7vux5j2d

7vux5j2d4#

在MDN上,他们为Chrome编写了以下内容:
从Chrome 46开始,update()返回一个解析为'undefined'的promise,如果操作成功完成或没有更新,并且拒绝失败。如果新worker运行但安装失败,promise仍然会解析。以前,它会引发一个异常。
这似乎已经发生了变化。在Chromium 77中运行update方法,我得到一个ServiceWorkerRegistration对象。

编辑:我也在Firefox 69中尝试了,结果与Chromium 77相同。

相关问题