Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/friendly-dodos-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] type tweaks
22 changes: 8 additions & 14 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,9 @@ function update_types(config, routes, route) {
/** @type {string[]} */
const exports = [];

if (route.names.length > 0) {
const params = route.names.map((param) => `${param}: string`).join('; ');
declarations.push(
`interface RouteParams extends Partial<Record<string, string>> { ${params} }`
);
} else {
declarations.push(`interface RouteParams extends Partial<Record<string, string>> {}`);
}
declarations.push(
`type RouteParams = { ${route.names.map((param) => `${param}: string`).join('; ')} }`
);

if (route.layout || route.leaf) {
// These could also be placed in our public types, but it would bloat them unnecessarily and we may want to change these in the future
Expand Down Expand Up @@ -212,12 +207,11 @@ function update_types(config, routes, route) {
}
});

if (layout_params.size > 0) {
const params = Array.from(layout_params).map((param) => `${param}?: string`);
declarations.push(`interface LayoutParams extends RouteParams { ${params.join('; ')} }`);
} else {
declarations.push(`interface LayoutParams extends RouteParams {}`);
}
declarations.push(
`type LayoutParams = RouteParams & { ${Array.from(layout_params).map(
(param) => `${param}?: string`
)} }`
);

const {
exports: e,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -11,7 +11,7 @@ type OutputDataShape<T> = MaybeWithVoid<
Record<string, any>
>;
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
interface LayoutParams extends RouteParams {}
type LayoutParams = RouteParams & {};
type LayoutParentData = EnsureParentData<{}>;

export type LayoutServerData = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -12,7 +12,7 @@ type OutputDataShape<T> = MaybeWithVoid<
>;
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
type PageParentData = EnsureParentData<import('../$types.js').LayoutData>;
interface LayoutParams extends RouteParams {}
type LayoutParams = RouteParams & {};
type LayoutServerParentData = EnsureParentData<import('../$types.js').LayoutServerData>;
type LayoutParentData = EnsureParentData<import('../$types.js').LayoutData>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -13,7 +13,7 @@ type OutputDataShape<T> = MaybeWithVoid<
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
type PageServerParentData = EnsureParentData<LayoutServerData>;
type PageParentData = EnsureParentData<LayoutData>;
interface LayoutParams extends RouteParams {}
type LayoutParams = RouteParams & {};
type LayoutServerParentData = EnsureParentData<{}>;
type LayoutParentData = EnsureParentData<{}>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -13,7 +13,7 @@ type OutputDataShape<T> = MaybeWithVoid<
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
type PageServerParentData = EnsureParentData<LayoutServerData>;
type PageParentData = EnsureParentData<LayoutData>;
interface LayoutParams extends RouteParams {}
type LayoutParams = RouteParams & {};
type LayoutParentData = EnsureParentData<{}>;

export type PageServerLoad<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -13,7 +13,7 @@ type OutputDataShape<T> = MaybeWithVoid<
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
type PageServerParentData = EnsureParentData<LayoutServerData>;
type PageParentData = EnsureParentData<LayoutData>;
interface LayoutParams extends RouteParams {}
type LayoutParams = RouteParams & {};
type LayoutParentData = EnsureParentData<{}>;

export type PageServerLoad<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -12,7 +12,7 @@ type OutputDataShape<T> = MaybeWithVoid<
>;
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
type PageParentData = EnsureParentData<LayoutData>;
interface LayoutParams extends RouteParams {}
type LayoutParams = RouteParams & {};
type LayoutParentData = EnsureParentData<{}>;

export type PageServerData = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -11,10 +11,7 @@ type OutputDataShape<T> = MaybeWithVoid<
Record<string, any>
>;
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
interface LayoutParams extends RouteParams {
rest?: string;
slug?: string;
}
type LayoutParams = RouteParams & { rest?: string; slug?: string };
type LayoutParentData = EnsureParentData<{}>;

export type LayoutServerData = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -11,9 +11,7 @@ type OutputDataShape<T> = MaybeWithVoid<
Record<string, any>
>;
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
interface LayoutParams extends RouteParams {
rest?: string;
}
type LayoutParams = RouteParams & { rest?: string };
type LayoutParentData = EnsureParentData<import('../$types.js').LayoutData>;

export type LayoutServerData = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {
rest: string;
}
type RouteParams = { rest: string };
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {
slug: string;
}
type RouteParams = { slug: string };
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {}
type RouteParams = {};
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand All @@ -11,10 +11,7 @@ type OutputDataShape<T> = MaybeWithVoid<
Record<string, any>
>;
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
interface LayoutParams extends RouteParams {
rest?: string;
slug?: string;
}
type LayoutParams = RouteParams & { rest?: string; slug?: string };
type LayoutParentData = EnsureParentData<{}>;

export type LayoutServerData = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {
rest: string;
}
type RouteParams = { rest: string };
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type * as Kit from '@sveltejs/kit';

interface RouteParams extends Partial<Record<string, string>> {
slug: string;
}
type RouteParams = { slug: string };
type MaybeWithVoid<T> = {} extends T ? T | void : T;
export type RequiredKeys<T> = {
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
Expand Down
14 changes: 8 additions & 6 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ export interface HandleError {
*/
export interface Load<
Params extends Partial<Record<string, string>> = Partial<Record<string, string>>,
InputData extends Record<string, any> | null = Record<string, any> | null,
ParentData extends Record<string, any> = Record<string, any>,
OutputData extends Record<string, any> | void = Record<string, any> | void
InputData extends Record<string, unknown> | null = Record<string, any> | null,
ParentData extends Record<string, unknown> = Record<string, any>,
OutputData extends Record<string, unknown> | void = Record<string, any> | void
> {
(event: LoadEvent<Params, InputData, ParentData>): MaybePromise<OutputData>;
}

export interface LoadEvent<
Params extends Partial<Record<string, string>> = Partial<Record<string, string>>,
Data extends Record<string, any> | null = Record<string, any> | null,
ParentData extends Record<string, any> = Record<string, any>
Data extends Record<string, unknown> | null = Record<string, any> | null,
ParentData extends Record<string, unknown> = Record<string, any>
> {
fetch(info: RequestInfo, init?: RequestInit): Promise<Response>;
params: Params;
Expand Down Expand Up @@ -253,7 +253,9 @@ export interface RequestEvent<
*
* It receives `Params` as the first generic argument, which you can skip by using [generated types](/docs/types#generated-types) instead.
*/
export interface RequestHandler<Params extends Record<string, string> = Record<string, string>> {
export interface RequestHandler<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dummdidumm shouldn't this be

export interface RequestHandler<
	Params extends Record<string, string> = Record<string, string>
> {
	(event: RequestEvent<Partial<Params>>): MaybePromise<Response>;
}

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly?

Copy link
Contributor

@ivanhofer ivanhofer Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you want to makr all params of the event as optional

Here is an example that demonstrates the issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. No it's working as intended. What you pass for the generic should be applied as-is, i.e. required params should be passed and can be expected to exist. It changes to Partial<..> so that in case you don't type the generic you can't just access the park without checking if it exists (in strict mode)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I get it. Thanks for the explanation. Didn't knew this trick before.

Params extends Partial<Record<string, string>> = Partial<Record<string, string>>
> {
(event: RequestEvent<Params>): MaybePromise<Response>;
}

Expand Down