Skip to content

Commit d967b94

Browse files
authored
Update typing of "self" without redeclaration in serviceworker
1 parent c158d2c commit d967b94

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

documentation/docs/30-advanced/40-service-workers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ Setting up proper types for service workers requires some manual setup. Inside y
132132
/// <reference lib="esnext" />
133133
/// <reference lib="webworker" />
134134
135-
const sw = /** @type {ServiceWorkerGlobalScope} */ (/** @type {unknown} */ (self));
135+
/** @typedef {ServiceWorkerGlobalScope} self */
136136
```
137137
```generated-ts
138138
/// <reference types="@sveltejs/kit" />
139139
/// <reference no-default-lib="true"/>
140140
/// <reference lib="esnext" />
141141
/// <reference lib="webworker" />
142142
143-
const sw = self as unknown as ServiceWorkerGlobalScope;
143+
declare const self: ServiceWorkerGlobalScope;
144144
```
145145

146-
This disables access to DOM typings like `HTMLElement` which are not available inside a service worker and instantiates the correct globals. The reassignment of `self` to `sw` allows you to type cast it in the process (there are a couple of ways to do this, but this is the easiest that requires no additional files). Use `sw` instead of `self` in the rest of the file. The reference to the SvelteKit types ensures that the `$service-worker` import has proper type definitions. If you import `$env/static/public` you either have to `// @ts-ignore` the import or add `/// <reference types="../.svelte-kit/ambient.d.ts" />` to the reference types.
146+
This disables access to DOM typings like `HTMLElement` which are not available inside a service worker and instantiates the correct globals. The reference to the SvelteKit types ensures that the `$service-worker` import has proper type definitions. If you import `$env/static/public` you either have to `// @ts-ignore` the import or add `/// <reference types="../.svelte-kit/ambient.d.ts" />` to the reference types.
147147

148148
## Other solutions
149149

0 commit comments

Comments
 (0)