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

[chore] convert remaining type aliases #2018

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
6 changes: 4 additions & 2 deletions packages/kit/types/endpoint.d.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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 Index<T = any> = Record<string, 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>;
14 changes: 9 additions & 5 deletions packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
@@ -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>;
}
92 changes: 46 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 { Location as Page, MaybePromise, InferValue, Index } from './helper';

export interface LoadInput<
PageParams extends Record<string, string> = Record<string, string>,
Context extends Record<string, any> = Record<string, any>,
PageParams extends Index<string> = Index<string>,
Context extends Index = Index,
Session = any
> {
page: Page<PageParams>;
@@ -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 Index<string> = Index<string>,
Context extends Index = Index,
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 Index = Index, Context extends Index = Index> {
status?: number;
error?: string | Error;
redirect?: string;
@@ -32,43 +29,46 @@ 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?: Index;
pageParams?: Index<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?: Index;
props?: Index;
}

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

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

export { Page };