Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate types for src/service-worker.js #8127

Closed
kwangure opened this issue Dec 13, 2022 · 2 comments · Fixed by #8508
Closed

Generate types for src/service-worker.js #8127

kwangure opened this issue Dec 13, 2022 · 2 comments · Fixed by #8508

Comments

@kwangure
Copy link
Contributor

kwangure commented Dec 13, 2022

Describe the problem

At present you need to add types for the service worker to clue TypeScript in about the WebWorker scope and set self to ServiceWorkerGlobalScope.

Describe the proposed solution

SvelteKit should generate types into the types folder.

Alternatives considered

Writing my own types. It typically involves adding triple-slash directives in my src/service-worker.js.

/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />

self.addEventListener('install', (event) => {
	/** @type {ExtendableEvent} */ (event).SpecialServiceWorkerStuff

And creating src/service-worker.d.ts

declare const self: ServiceWorkerGlobalScope;

Importance

nice to have

Additional Information

TS Context: microsoft/TypeScript#14877.

@dummdidumm dummdidumm added this to the post-1.0 milestone Dec 13, 2022
@dummdidumm
Copy link
Member

We can't generate such types into the types folder because they still need to be manually referenced inside service-worker.js, and at the same time it needs to be made sure that those types are not loaded anywhere else, since the no-default-lib instruction effectively removes theses type definitions from the rest of the project, which is undesireable. I think we should instead document this pattern and adjust our generated tsconfig.json to exclude the service worker file, so it doesn't interfere with the rest of the project.

dummdidumm added a commit that referenced this issue Jan 13, 2023
Also document how to get types working inside the file
closes #8127
Rich-Harris added a commit that referenced this issue Jan 13, 2023
* fix: exclude service worker from tsconfig

Also document how to get types working inside the file
closes #8127

* ws -> sw

Co-authored-by: Rich Harris <hello@rich-harris.dev>
@jdgamble555
Copy link

The problem with this is that if you import anything inside the /src/service-worker folder, it won't find things like $lib etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants