Skip to content

Commit

Permalink
[chore] convert remaining type aliases (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Jul 28, 2021
1 parent b18a45c commit e23ae18
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
let random = 0;

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

export function get() {
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export interface EndpointOutput<Body extends DefaultBody = DefaultBody> {
body?: Body;
}

export type RequestHandler<
export interface RequestHandler<
Locals = Record<string, any>,
Input = unknown,
Output extends DefaultBody = DefaultBody
> = (request: ServerRequest<Locals, Input>) => MaybePromise<void | EndpointOutput<Output>>;
> {
(request: ServerRequest<Locals, Input>): MaybePromise<void | EndpointOutput<Output>>;
}
3 changes: 2 additions & 1 deletion packages/kit/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export type Location<Params extends Record<string, string> = Record<string, stri
query: URLSearchParams;
};

export type MaybePromise<T> = T | Promise<T>;
export type InferValue<T, Key extends keyof T, Default> = T extends Record<Key, infer Val>
? Val
: Default;
export type MaybePromise<T> = T | Promise<T>;
export type Rec<T = any> = Record<string, T>;
14 changes: 9 additions & 5 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export interface GetSession<Locals = Record<string, any>, Session = any> {
(request: ServerRequest<Locals>): MaybePromise<Session>;
}

export type Handle<Locals = Record<string, any>> = (input: {
request: ServerRequest<Locals>;
resolve: (request: ServerRequest<Locals>) => MaybePromise<ServerResponse>;
}) => MaybePromise<ServerResponse>;
export interface Handle<Locals = Record<string, any>> {
(input: {
request: ServerRequest<Locals>;
resolve: (request: ServerRequest<Locals>) => MaybePromise<ServerResponse>;
}): MaybePromise<ServerResponse>;
}

export type ServerFetch = (req: Request) => Promise<Response>;
export interface ServerFetch {
(req: Request): Promise<Response>;
}
90 changes: 44 additions & 46 deletions packages/kit/types/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Location as Page, MaybePromise, InferValue } from './helper';
import { InferValue, Location as Page, MaybePromise, Rec } from './helper';

export interface LoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>,
PageParams extends Rec<string> = Rec<string>,
Context extends Rec = Rec,
Session = any
> {
page: Page<PageParams>;
Expand All @@ -12,18 +12,15 @@ export interface LoadInput<
}

export interface ErrorLoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>,
PageParams extends Rec<string> = Rec<string>,
Context extends Rec = Rec,
Session = any
> extends LoadInput<PageParams, Context, Session> {
status?: number;
error?: Error;
}

export interface LoadOutput<
Props extends Record<string, any> = Record<string, any>,
Context extends Record<string, any> = Record<string, any>
> {
export interface LoadOutput<Props extends Rec = Rec, Context extends Rec = Rec> {
status?: number;
error?: string | Error;
redirect?: string;
Expand All @@ -32,43 +29,44 @@ export interface LoadOutput<
maxage?: number;
}

// Publicized Types
export type Load<
Input extends {
context?: Record<string, any>;
pageParams?: Record<string, string>;
session?: any;
} = {},
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = (
input: LoadInput<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'context', Record<string, any>>,
InferValue<Input, 'session', any>
>
) => MaybePromise<void | LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'context', Record<string, any>>
>>;
interface LoadInputExtends {
context?: Rec;
pageParams?: Rec<string>;
session?: any;
}

export type ErrorLoad<
Input extends {
context?: Record<string, any>;
pageParams?: Record<string, string>;
session?: any;
} = {},
Output extends { context?: Record<string, any>; props?: Record<string, any> } = {}
> = (
input: ErrorLoadInput<
InferValue<Input, 'pageParams', Record<string, string>>,
InferValue<Input, 'context', Record<string, any>>,
InferValue<Input, 'session', any>
>
) => MaybePromise<
LoadOutput<
InferValue<Output, 'props', Record<string, any>>,
InferValue<Output, 'context', Record<string, any>>
>
>;
interface LoadOutputExtends {
context?: Rec;
props?: Rec;
}

export interface Load<
Input extends LoadInputExtends = Required<LoadInputExtends>,
Output extends LoadOutputExtends = Required<LoadOutputExtends>
> {
(
input: LoadInput<
InferValue<Input, 'pageParams', Rec<string>>,
InferValue<Input, 'context', Rec>,
InferValue<Input, 'session', any>
>
): MaybePromise<void | LoadOutput<
InferValue<Output, 'props', Rec>,
InferValue<Output, 'context', Rec>
>>;
}

export interface ErrorLoad<
Input extends LoadInputExtends = Required<LoadInputExtends>,
Output extends LoadOutputExtends = Required<LoadOutputExtends>
> {
(
input: ErrorLoadInput<
InferValue<Input, 'pageParams', Rec<string>>,
InferValue<Input, 'context', Rec>,
InferValue<Input, 'session', any>
>
): MaybePromise<LoadOutput<InferValue<Output, 'props', Rec>, InferValue<Output, 'context', Rec>>>;
}

export { Page };

0 comments on commit e23ae18

Please sign in to comment.