Skip to content

Commit e2a4538

Browse files
authored
fix: use arrow function types over bound functions (#12955)
Using class method function types causes SvelteKit users to get TypeScript ESLint errors if they're using the [@typescript-eslint/unbound-method][1] rule (included by default in the `recommended-type-checked` config). [1]: https://typescript-eslint.io/rules/unbound-method/ The following types in `public.d.ts` are still using non-arrow functions, since - These functions are for user inputs, which might rely on these functions being bound, and so it might be a breaking change to change these types: - [`Emulator['platform']`][1] - functions in `KitConfig` - Class methods that rely on `this`: - [`Server`][2] [1]: https://github.com/sveltejs/kit/blob/b25f2d052bbf22e832ac57cbf9e12c48ac922f96/packages/kit/src/exports/public.d.ts#L272-L276 [2]: https://github.com/sveltejs/kit/blob/b25f2d052bbf22e832ac57cbf9e12c48ac922f96/packages/kit/src/exports/public.d.ts#L1174-L1178 fixes #12622
1 parent b60707c commit e2a4538

File tree

4 files changed

+93
-88
lines changed

4 files changed

+93
-88
lines changed

.changeset/wet-dancers-kneel.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: use arrow function types over bound funcs

packages/kit/src/exports/public.d.ts

+43-43
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface Adapter {
3434
* This function is called after SvelteKit has built your app.
3535
* @param builder An object provided by SvelteKit that contains methods for adapting the app
3636
*/
37-
adapt(builder: Builder): MaybePromise<void>;
37+
adapt: (builder: Builder) => MaybePromise<void>;
3838
/**
3939
* Checks called during dev and build to determine whether specific features will work in production with this adapter
4040
*/
@@ -49,7 +49,7 @@ export interface Adapter {
4949
* Creates an `Emulator`, which allows the adapter to influence the environment
5050
* during dev, build and prerendering
5151
*/
52-
emulate?(): MaybePromise<Emulator>;
52+
emulate?: () => MaybePromise<Emulator>;
5353
}
5454

5555
export type LoadProperties<input extends Record<string, any> | void> = input extends void
@@ -94,9 +94,9 @@ export interface Builder {
9494
/** Print messages to the console. `log.info` and `log.minor` are silent unless Vite's `logLevel` is `info`. */
9595
log: Logger;
9696
/** Remove `dir` and all its contents. */
97-
rimraf(dir: string): void;
97+
rimraf: (dir: string) => void;
9898
/** Create `dir` and any required parent directories. */
99-
mkdirp(dir: string): void;
99+
mkdirp: (dir: string) => void;
100100

101101
/** The fully resolved `svelte.config.js`. */
102102
config: ValidatedConfig;
@@ -111,59 +111,59 @@ export interface Builder {
111111
* @param fn A function that groups a set of routes into an entry point
112112
* @deprecated Use `builder.routes` instead
113113
*/
114-
createEntries(fn: (route: RouteDefinition) => AdapterEntry): Promise<void>;
114+
createEntries: (fn: (route: RouteDefinition) => AdapterEntry) => Promise<void>;
115115

116116
/**
117117
* Find all the assets imported by server files belonging to `routes`
118118
*/
119-
findServerAssets(routes: RouteDefinition[]): string[];
119+
findServerAssets: (routes: RouteDefinition[]) => string[];
120120

121121
/**
122122
* Generate a fallback page for a static webserver to use when no route is matched. Useful for single-page apps.
123123
*/
124-
generateFallback(dest: string): Promise<void>;
124+
generateFallback: (dest: string) => Promise<void>;
125125

126126
/**
127127
* Generate a module exposing build-time environment variables as `$env/dynamic/public`.
128128
*/
129-
generateEnvModule(): void;
129+
generateEnvModule: () => void;
130130

131131
/**
132132
* Generate a server-side manifest to initialise the SvelteKit [server](https://svelte.dev/docs/kit/@sveltejs-kit#Server) with.
133133
* @param opts a relative path to the base directory of the app and optionally in which format (esm or cjs) the manifest should be generated
134134
*/
135-
generateManifest(opts: { relativePath: string; routes?: RouteDefinition[] }): string;
135+
generateManifest: (opts: { relativePath: string; routes?: RouteDefinition[] }) => string;
136136

137137
/**
138138
* Resolve a path to the `name` directory inside `outDir`, e.g. `/path/to/.svelte-kit/my-adapter`.
139139
* @param name path to the file, relative to the build directory
140140
*/
141-
getBuildDirectory(name: string): string;
141+
getBuildDirectory: (name: string) => string;
142142
/** Get the fully resolved path to the directory containing client-side assets, including the contents of your `static` directory. */
143-
getClientDirectory(): string;
143+
getClientDirectory: () => string;
144144
/** Get the fully resolved path to the directory containing server-side code. */
145-
getServerDirectory(): string;
145+
getServerDirectory: () => string;
146146
/** Get the application path including any configured `base` path, e.g. `my-base-path/_app`. */
147-
getAppPath(): string;
147+
getAppPath: () => string;
148148

149149
/**
150150
* Write client assets to `dest`.
151151
* @param dest the destination folder
152152
* @returns an array of files written to `dest`
153153
*/
154-
writeClient(dest: string): string[];
154+
writeClient: (dest: string) => string[];
155155
/**
156156
* Write prerendered files to `dest`.
157157
* @param dest the destination folder
158158
* @returns an array of files written to `dest`
159159
*/
160-
writePrerendered(dest: string): string[];
160+
writePrerendered: (dest: string) => string[];
161161
/**
162162
* Write server-side code to `dest`.
163163
* @param dest the destination folder
164164
* @returns an array of files written to `dest`
165165
*/
166-
writeServer(dest: string): string[];
166+
writeServer: (dest: string) => string[];
167167
/**
168168
* Copy a file or directory.
169169
* @param from the source file or directory
@@ -172,20 +172,20 @@ export interface Builder {
172172
* @param opts.replace a map of strings to replace
173173
* @returns an array of files that were copied
174174
*/
175-
copy(
175+
copy: (
176176
from: string,
177177
to: string,
178178
opts?: {
179179
filter?(basename: string): boolean;
180180
replace?: Record<string, string>;
181181
}
182-
): string[];
182+
) => string[];
183183

184184
/**
185185
* Compress files in `directory` with gzip and brotli, where appropriate. Generates `.gz` and `.br` files alongside the originals.
186186
* @param {string} directory The directory containing the files to be compressed
187187
*/
188-
compress(directory: string): Promise<void>;
188+
compress: (directory: string) => Promise<void>;
189189
}
190190

191191
export interface Config {
@@ -215,13 +215,13 @@ export interface Cookies {
215215
* @param name the name of the cookie
216216
* @param opts the options, passed directly to `cookie.parse`. See documentation [here](https://github.com/jshttp/cookie#cookieparsestr-options)
217217
*/
218-
get(name: string, opts?: import('cookie').CookieParseOptions): string | undefined;
218+
get: (name: string, opts?: import('cookie').CookieParseOptions) => string | undefined;
219219

220220
/**
221221
* Gets all cookies that were previously set with `cookies.set`, or from the request headers.
222222
* @param opts the options, passed directly to `cookie.parse`. See documentation [here](https://github.com/jshttp/cookie#cookieparsestr-options)
223223
*/
224-
getAll(opts?: import('cookie').CookieParseOptions): Array<{ name: string; value: string }>;
224+
getAll: (opts?: import('cookie').CookieParseOptions) => Array<{ name: string; value: string }>;
225225

226226
/**
227227
* Sets a cookie. This will add a `set-cookie` header to the response, but also make the cookie available via `cookies.get` or `cookies.getAll` during the current request.
@@ -233,11 +233,11 @@ export interface Cookies {
233233
* @param value the cookie value
234234
* @param opts the options, passed directly to `cookie.serialize`. See documentation [here](https://github.com/jshttp/cookie#cookieserializename-value-options)
235235
*/
236-
set(
236+
set: (
237237
name: string,
238238
value: string,
239239
opts: import('cookie').CookieSerializeOptions & { path: string }
240-
): void;
240+
) => void;
241241

242242
/**
243243
* Deletes a cookie by setting its value to an empty string and setting the expiry date in the past.
@@ -246,7 +246,7 @@ export interface Cookies {
246246
* @param name the name of the cookie
247247
* @param opts the options, passed directly to `cookie.serialize`. The `path` must match the path of the cookie you want to delete. See documentation [here](https://github.com/jshttp/cookie#cookieserializename-value-options)
248248
*/
249-
delete(name: string, opts: import('cookie').CookieSerializeOptions & { path: string }): void;
249+
delete: (name: string, opts: import('cookie').CookieSerializeOptions & { path: string }) => void;
250250

251251
/**
252252
* Serialize a cookie name-value pair into a `Set-Cookie` header string, but don't apply it to the response.
@@ -259,11 +259,11 @@ export interface Cookies {
259259
* @param value the cookie value
260260
* @param opts the options, passed directly to `cookie.serialize`. See documentation [here](https://github.com/jshttp/cookie#cookieserializename-value-options)
261261
*/
262-
serialize(
262+
serialize: (
263263
name: string,
264264
value: string,
265265
opts: import('cookie').CookieSerializeOptions & { path: string }
266-
): string;
266+
) => string;
267267
}
268268

269269
/**
@@ -738,7 +738,7 @@ export interface KitConfig {
738738
*/
739739
export type Handle = (input: {
740740
event: RequestEvent;
741-
resolve(event: RequestEvent, opts?: ResolveOptions): MaybePromise<Response>;
741+
resolve: (event: RequestEvent, opts?: ResolveOptions) => MaybePromise<Response>;
742742
}) => MaybePromise<Response>;
743743

744744
/**
@@ -893,14 +893,14 @@ export interface LoadEvent<
893893
*
894894
* `setHeaders` has no effect when a `load` function runs in the browser.
895895
*/
896-
setHeaders(headers: Record<string, string>): void;
896+
setHeaders: (headers: Record<string, string>) => void;
897897
/**
898898
* `await parent()` returns data from parent `+layout.js` `load` functions.
899899
* Implicitly, a missing `+layout.js` is treated as a `({ data }) => data` function, meaning that it will return and forward data from parent `+layout.server.js` files.
900900
*
901901
* Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
902902
*/
903-
parent(): Promise<ParentData>;
903+
parent: () => Promise<ParentData>;
904904
/**
905905
* This function declares that the `load` function has a _dependency_ on one or more URLs or custom identifiers, which can subsequently be used with [`invalidate()`](https://svelte.dev/docs/kit/$app-navigation#invalidate) to cause `load` to rerun.
906906
*
@@ -938,7 +938,7 @@ export interface LoadEvent<
938938
* <button on:click={increase}>Increase Count</button>
939939
* ```
940940
*/
941-
depends(...deps: Array<`${string}:${string}`>): void;
941+
depends: (...deps: Array<`${string}:${string}`>) => void;
942942
/**
943943
* Use this function to opt out of dependency tracking for everything that is synchronously called within the callback. Example:
944944
*
@@ -952,7 +952,7 @@ export interface LoadEvent<
952952
* }
953953
* ```
954954
*/
955-
untrack<T>(fn: () => T): T;
955+
untrack: <T>(fn: () => T) => T;
956956
}
957957

958958
export interface NavigationEvent<
@@ -1047,7 +1047,7 @@ export interface BeforeNavigate extends Navigation {
10471047
/**
10481048
* Call this to prevent the navigation from starting.
10491049
*/
1050-
cancel(): void;
1050+
cancel: () => void;
10511051
}
10521052

10531053
/**
@@ -1161,7 +1161,7 @@ export interface RequestEvent<
11611161
/**
11621162
* The client's IP address, set by the adapter.
11631163
*/
1164-
getClientAddress(): string;
1164+
getClientAddress: () => string;
11651165
/**
11661166
* Contains custom data that was added to the request within the [`server handle hook`](https://svelte.dev/docs/kit/hooks#Server-hooks-handle).
11671167
*/
@@ -1209,7 +1209,7 @@ export interface RequestEvent<
12091209
*
12101210
* You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](https://svelte.dev/docs/kit/@sveltejs-kit#Cookies) API instead.
12111211
*/
1212-
setHeaders(headers: Record<string, string>): void;
1212+
setHeaders: (headers: Record<string, string>) => void;
12131213
/**
12141214
* The requested URL.
12151215
*/
@@ -1242,20 +1242,20 @@ export interface ResolveOptions {
12421242
* but they will always be split at sensible boundaries such as `%sveltekit.head%` or layout/page components.
12431243
* @param input the html chunk and the info if this is the last chunk
12441244
*/
1245-
transformPageChunk?(input: { html: string; done: boolean }): MaybePromise<string | undefined>;
1245+
transformPageChunk?: (input: { html: string; done: boolean }) => MaybePromise<string | undefined>;
12461246
/**
12471247
* Determines which headers should be included in serialized responses when a `load` function loads a resource with `fetch`.
12481248
* By default, none will be included.
12491249
* @param name header name
12501250
* @param value header value
12511251
*/
1252-
filterSerializedResponseHeaders?(name: string, value: string): boolean;
1252+
filterSerializedResponseHeaders?: (name: string, value: string) => boolean;
12531253
/**
12541254
* Determines what should be added to the `<head>` tag to preload it.
12551255
* By default, `js` and `css` files will be preloaded.
12561256
* @param input the type of the file and its path
12571257
*/
1258-
preload?(input: { type: 'font' | 'css' | 'js' | 'asset'; path: string }): boolean;
1258+
preload?: (input: { type: 'font' | 'css' | 'js' | 'asset'; path: string }) => boolean;
12591259
}
12601260

12611261
export interface RouteDefinition<Config = any> {
@@ -1297,7 +1297,7 @@ export interface SSRManifest {
12971297
client: NonNullable<BuildData['client']>;
12981298
nodes: SSRNodeLoader[];
12991299
routes: SSRRoute[];
1300-
matchers(): Promise<Record<string, ParamMatcher>>;
1300+
matchers: () => Promise<Record<string, ParamMatcher>>;
13011301
/** A `[file]: size` map of all assets imported by server code */
13021302
server_assets: Record<string, number>;
13031303
};
@@ -1324,7 +1324,7 @@ export interface ServerLoadEvent<
13241324
*
13251325
* Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
13261326
*/
1327-
parent(): Promise<ParentData>;
1327+
parent: () => Promise<ParentData>;
13281328
/**
13291329
* This function declares that the `load` function has a _dependency_ on one or more URLs or custom identifiers, which can subsequently be used with [`invalidate()`](https://svelte.dev/docs/kit/$app-navigation#invalidate) to cause `load` to rerun.
13301330
*
@@ -1362,7 +1362,7 @@ export interface ServerLoadEvent<
13621362
* <button on:click={increase}>Increase Count</button>
13631363
* ```
13641364
*/
1365-
depends(...deps: string[]): void;
1365+
depends: (...deps: string[]) => void;
13661366
/**
13671367
* Use this function to opt out of dependency tracking for everything that is synchronously called within the callback. Example:
13681368
*
@@ -1376,7 +1376,7 @@ export interface ServerLoadEvent<
13761376
* }
13771377
* ```
13781378
*/
1379-
untrack<T>(fn: () => T): T;
1379+
untrack: <T>(fn: () => T) => T;
13801380
}
13811381

13821382
/**
@@ -1447,7 +1447,7 @@ export type SubmitFunction<
14471447
formElement: HTMLFormElement;
14481448
controller: AbortController;
14491449
submitter: HTMLElement | null;
1450-
cancel(): void;
1450+
cancel: () => void;
14511451
}) => MaybePromise<
14521452
| void
14531453
| ((opts: {
@@ -1460,7 +1460,7 @@ export type SubmitFunction<
14601460
* @param options Set `reset: false` if you don't want the `<form>` values to be reset after a successful submission.
14611461
* @param invalidateAll Set `invalidateAll: false` if you don't want the action to call `invalidateAll` after submission.
14621462
*/
1463-
update(options?: { reset?: boolean; invalidateAll?: boolean }): Promise<void>;
1463+
update: (options?: { reset?: boolean; invalidateAll?: boolean }) => Promise<void>;
14641464
}) => void)
14651465
>;
14661466

packages/kit/src/runtime/server/cookie.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function get_cookies(request, url, trailing_slash) {
5555

5656
/**
5757
* @param {string} name
58-
* @param {import('cookie').CookieParseOptions} opts
58+
* @param {import('cookie').CookieParseOptions} [opts]
5959
*/
6060
get(name, opts) {
6161
const c = new_cookies[name];
@@ -91,7 +91,7 @@ export function get_cookies(request, url, trailing_slash) {
9191
},
9292

9393
/**
94-
* @param {import('cookie').CookieParseOptions} opts
94+
* @param {import('cookie').CookieParseOptions} [opts]
9595
*/
9696
getAll(opts) {
9797
const cookies = parse(header, { decode: opts?.decode });

0 commit comments

Comments
 (0)