VSCode Typescript 智能感知错误

Dut*_*lie 3 javascript typescript visual-studio-code svelte

我一直在尝试使用 SvelteKit 为我的网站创建 ServiceWorker,但在这里遇到了问题。我创建了一个文件/src/service-worker.ts,并在其中添加了以下代码

import { build, files, prerendered, version } from '$service-worker';

const applicationCache = `applicationCache-v${version}`;
const staticCache = `staticCache-v${version}`;

const returnSSRpage = (path) =>
  caches.open("ssrCache").then((cache) => cache.match(path));

// Caches the svelte app (not the data)
self.addEventListener("install", (event) => {
  event.waitUntil(
    Promise.all([
      caches
        .open("ssrCache")
        .then((cache) => cache.addAll(["/"])),
      caches
        .open(applicationCache)
        .then((cache) => cache.addAll(build)),
      caches
        .open(staticCache)
        .then((cache) => cache.addAll(files))
    ])
      .then(self.skipWaiting()),
  )
})
... reduced code
Run Code Online (Sandbox Code Playgroud)

运行npm run build此代码时,编译效果非常好,并且代码在浏览器中运行。然而,我的 VSCode 智能感知出现了一些错误。最值得注意的是,它说 的waitUntil属性event不存在。 Property 'waitUntil' does not exist on type 'Event'.ts(2339)除其他事项外,例如Property 'skipWaiting' does not exist on type 'Window & typeof globalThis'.ts(2339)Cannot find name 'clients'.ts(2304)

现在,我对 Javascript 和 Typescript 还很陌生,但根据我的经验,Intellisense 不应该输出编译期间不会出现的错误。为什么会出现这种情况?

我不确定要提供什么信息。我的 TS 版本是 4.7.4,这也是 VSCode 用于 Intellisense 的版本。我已经安装了 JS 和 TS 的 ESLint 扩展。

这里可能有什么问题?谢谢!

H.B*_*.B. 6

您可以添加到"WebWorker"Service Worker 文件中并声明其类型:compilerOptions.libtsconfig.jsonself

declare var self: ServiceWorkerGlobalScope;
Run Code Online (Sandbox Code Playgroud)

这将导致自动推断事件类型,而无需通过事件名称进行进一步注释。

您可能需要重新启动 TS 服务器(有一个命令:)TypeScript: Restart TS Server

不过,奇怪的是它会按原样构建......