Skip to content

Commit e23ae18

Browse files
authored
[chore] convert remaining type aliases (#2018)
1 parent b18a45c commit e23ae18

File tree

5 files changed

+61
-57
lines changed

5 files changed

+61
-57
lines changed

packages/kit/test/apps/basics/src/routes/routing/shadow/action.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
let random = 0;
22

3-
/** @type {import('@sveltejs/kit').RequestHandler} */
3+
/** @type {import('@sveltejs/kit').RequestHandler<any, FormData>} */
44
export function post({ body }) {
5-
// @ts-expect-error (TODO make the types work somehow)
6-
random = body.get('random');
5+
random = +body.get('random');
76
}
87

98
export function get() {

packages/kit/types/endpoint.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ export interface EndpointOutput<Body extends DefaultBody = DefaultBody> {
1818
body?: Body;
1919
}
2020

21-
export type RequestHandler<
21+
export interface RequestHandler<
2222
Locals = Record<string, any>,
2323
Input = unknown,
2424
Output extends DefaultBody = DefaultBody
25-
> = (request: ServerRequest<Locals, Input>) => MaybePromise<void | EndpointOutput<Output>>;
25+
> {
26+
(request: ServerRequest<Locals, Input>): MaybePromise<void | EndpointOutput<Output>>;
27+
}

packages/kit/types/helper.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export type Location<Params extends Record<string, string> = Record<string, stri
2626
query: URLSearchParams;
2727
};
2828

29-
export type MaybePromise<T> = T | Promise<T>;
3029
export type InferValue<T, Key extends keyof T, Default> = T extends Record<Key, infer Val>
3130
? Val
3231
: Default;
32+
export type MaybePromise<T> = T | Promise<T>;
33+
export type Rec<T = any> = Record<string, T>;

packages/kit/types/hooks.d.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ export interface GetSession<Locals = Record<string, any>, Session = any> {
2020
(request: ServerRequest<Locals>): MaybePromise<Session>;
2121
}
2222

23-
export type Handle<Locals = Record<string, any>> = (input: {
24-
request: ServerRequest<Locals>;
25-
resolve: (request: ServerRequest<Locals>) => MaybePromise<ServerResponse>;
26-
}) => MaybePromise<ServerResponse>;
23+
export interface Handle<Locals = Record<string, any>> {
24+
(input: {
25+
request: ServerRequest<Locals>;
26+
resolve: (request: ServerRequest<Locals>) => MaybePromise<ServerResponse>;
27+
}): MaybePromise<ServerResponse>;
28+
}
2729

28-
export type ServerFetch = (req: Request) => Promise<Response>;
30+
export interface ServerFetch {
31+
(req: Request): Promise<Response>;
32+
}

packages/kit/types/page.d.ts

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Location as Page, MaybePromise, InferValue } from './helper';
1+
import { InferValue, Location as Page, MaybePromise, Rec } from './helper';
22

33
export interface LoadInput<
4-
PageParams extends Record<string, string> = Record<string, string>,
5-
Context extends Record<string, any> = Record<string, any>,
4+
PageParams extends Rec<string> = Rec<string>,
5+
Context extends Rec = Rec,
66
Session = any
77
> {
88
page: Page<PageParams>;
@@ -12,18 +12,15 @@ export interface LoadInput<
1212
}
1313

1414
export interface ErrorLoadInput<
15-
PageParams extends Record<string, string> = Record<string, string>,
16-
Context extends Record<string, any> = Record<string, any>,
15+
PageParams extends Rec<string> = Rec<string>,
16+
Context extends Rec = Rec,
1717
Session = any
1818
> extends LoadInput<PageParams, Context, Session> {
1919
status?: number;
2020
error?: Error;
2121
}
2222

23-
export interface LoadOutput<
24-
Props extends Record<string, any> = Record<string, any>,
25-
Context extends Record<string, any> = Record<string, any>
26-
> {
23+
export interface LoadOutput<Props extends Rec = Rec, Context extends Rec = Rec> {
2724
status?: number;
2825
error?: string | Error;
2926
redirect?: string;
@@ -32,43 +29,44 @@ export interface LoadOutput<
3229
maxage?: number;
3330
}
3431

35-
// Publicized Types
36-
export type Load<
37-
Input extends {
38-
context?: Record<string, any>;
39-
pageParams?: Record<string, string>;
40-
session?: any;
41-
} = {},
42-
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
43-
> = (
44-
input: LoadInput<
45-
InferValue<Input, 'pageParams', Record<string, string>>,
46-
InferValue<Input, 'context', Record<string, any>>,
47-
InferValue<Input, 'session', any>
48-
>
49-
) => MaybePromise<void | LoadOutput<
50-
InferValue<Output, 'props', Record<string, any>>,
51-
InferValue<Output, 'context', Record<string, any>>
52-
>>;
32+
interface LoadInputExtends {
33+
context?: Rec;
34+
pageParams?: Rec<string>;
35+
session?: any;
36+
}
5337

54-
export type ErrorLoad<
55-
Input extends {
56-
context?: Record<string, any>;
57-
pageParams?: Record<string, string>;
58-
session?: any;
59-
} = {},
60-
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
61-
> = (
62-
input: ErrorLoadInput<
63-
InferValue<Input, 'pageParams', Record<string, string>>,
64-
InferValue<Input, 'context', Record<string, any>>,
65-
InferValue<Input, 'session', any>
66-
>
67-
) => MaybePromise<
68-
LoadOutput<
69-
InferValue<Output, 'props', Record<string, any>>,
70-
InferValue<Output, 'context', Record<string, any>>
71-
>
72-
>;
38+
interface LoadOutputExtends {
39+
context?: Rec;
40+
props?: Rec;
41+
}
42+
43+
export interface Load<
44+
Input extends LoadInputExtends = Required<LoadInputExtends>,
45+
Output extends LoadOutputExtends = Required<LoadOutputExtends>
46+
> {
47+
(
48+
input: LoadInput<
49+
InferValue<Input, 'pageParams', Rec<string>>,
50+
InferValue<Input, 'context', Rec>,
51+
InferValue<Input, 'session', any>
52+
>
53+
): MaybePromise<void | LoadOutput<
54+
InferValue<Output, 'props', Rec>,
55+
InferValue<Output, 'context', Rec>
56+
>>;
57+
}
58+
59+
export interface ErrorLoad<
60+
Input extends LoadInputExtends = Required<LoadInputExtends>,
61+
Output extends LoadOutputExtends = Required<LoadOutputExtends>
62+
> {
63+
(
64+
input: ErrorLoadInput<
65+
InferValue<Input, 'pageParams', Rec<string>>,
66+
InferValue<Input, 'context', Rec>,
67+
InferValue<Input, 'session', any>
68+
>
69+
): MaybePromise<LoadOutput<InferValue<Output, 'props', Rec>, InferValue<Output, 'context', Rec>>>;
70+
}
7371

7472
export { Page };

0 commit comments

Comments
 (0)