vscode typeScript Intellisense错误

发布于 2025-02-13 00:33:26 字数 1375 浏览 2 评论 0原文

我一直在尝试使用Sveltekit为我的网站创建服务工作人员,但是在这里遇到了一个问题。 放置了以下代码

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

我创建了一个文件/src/service-worker.ts,在其中,我在运行npm run build build此代码时 浏览器。但是,我的vscode Intellisense会发现一些错误。最值得注意的是,它说waituntil event的属性不存在。 属性'waituntil'在类型'event'.ts(2339)上不存在,例如属性'skipwaiting'在type'window& typeof globalthis'.ts(2339)找不到名称'clients'.ts(2304)

现在,我是JavaScript和Typescript的新手,但是根据我的经验,Intellisense不应输出在编译过程中也不会出现的错误。为什么会发生这种情况?

我不确定要提供的信息。我的TS版本是4.7.4,它也是Vscode版本用于Intellisense。我已经为JS和TS安装了ESLINT扩展名。

这里有什么问题? 谢谢!

I have been trying to create ServiceWorker for my website using SvelteKit, but am running into an issue here. I created a file /src/service-worker.ts and in there, I put the following code

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

When running npm run build this code compiles perfectly fine and the code runs in the browser. However, my VSCode intellisense gets some stuff wrong. Most notably, it says that the waitUntil property of event does not exist.
Property 'waitUntil' does not exist on type 'Event'.ts(2339) among other things, such as Property 'skipWaiting' does not exist on type 'Window & typeof globalThis'.ts(2339) and Cannot find name 'clients'.ts(2304).

Now, I am quite new to Javascript and Typescript, but from my experience, the Intellisense should not output an error that doesn't also appear during compilation. Why does this happen?

I am unsure of what information to provide. My TS version is 4.7.4 which is also the version VSCode is using for Intellisense. I have installed the ESLint extension for JS and TS.

What could be the problem here?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2025-02-20 00:33:26

您可以将“ webworker”添加到 compileroptions.lib tsconfig.json中,并在服务工作者中声明self的类型文件:

declare var self: ServiceWorkerGlobalScope;

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

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

尽管如此,它仍然会像...

You can add "WebWorker" to the compilerOptions.lib in tsconfig.json and declare the type of self in the service worker file:

declare var self: ServiceWorkerGlobalScope;

This will lead to the event types automatically being inferred without further annotations via the event name.

You may need to restart the TS server (there is a command for that: TypeScript: Restart TS Server).

Still, odd that it would build as is...

轻许诺言 2025-02-20 00:33:26

这对我来说非常有效:

const sw: ServiceWorkerGlobalScope = self as unknown as ServiceWorkerGlobalScope;

现在您将self替换为sw,哪个更有意义,而且您获得了正确的类型。

This worked very well for me:

const sw: ServiceWorkerGlobalScope = self as unknown as ServiceWorkerGlobalScope;

Now you replace self with sw, which kinda makes more sense, and also you get correct types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文