Skip to content

Commit 7edcf0e

Browse files
committed
generate types
1 parent e2e52a3 commit 7edcf0e

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

packages/kit/src/core/sync/write_types/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ function update_types(config, routes, route, to_delete = new Set()) {
195195
// Makes sure a type is "repackaged" and therefore more readable
196196
declarations.push('type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;');
197197
declarations.push(
198-
`type RouteParams = { ${route.names.map((param) => `${param}: string`).join('; ')} }`
198+
`type RouteParams = { ${route.names
199+
.map((param, idx) => `${param}${route.optional[idx] ? '?' : ''}: string`)
200+
.join('; ')} }`
199201
);
200202

201203
// 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

packages/kit/src/core/sync/write_types/test/slugs/_expected/$types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type OutputDataShape<T> = MaybeWithVoid<
1212
Record<string, any>
1313
>;
1414
type EnsureDefined<T> = T extends null | undefined ? {} : T;
15-
type LayoutParams = RouteParams & { rest?: string; slug?: string; optional?: string | void };
15+
type LayoutParams = RouteParams & { optional?: string; rest?: string; slug?: string };
1616
type LayoutParentData = EnsureDefined<{}>;
1717

1818
export type LayoutServerData = null;
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
import type * as Kit from '@sveltejs/kit';
22

33
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
4-
type RouteParams = { optional: string | void }
4+
type RouteParams = { optional?: string };
55
type MaybeWithVoid<T> = {} extends T ? T | void : T;
6-
export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K; }[keyof T];
7-
type OutputDataShape<T> = MaybeWithVoid<Omit<App.PageData, RequiredKeys<T>> & Partial<Pick<App.PageData, keyof T & keyof App.PageData>> & Record<string, any>>
6+
export type RequiredKeys<T> = {
7+
[K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K;
8+
}[keyof T];
9+
type OutputDataShape<T> = MaybeWithVoid<
10+
Omit<App.PageData, RequiredKeys<T>> &
11+
Partial<Pick<App.PageData, keyof T & keyof App.PageData>> &
12+
Record<string, any>
13+
>;
814
type EnsureDefined<T> = T extends null | undefined ? {} : T;
915
type PageParentData = EnsureDefined<import('../../$types.js').LayoutData>;
1016

1117
export type PageServerData = null;
12-
export type PageLoad<OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
18+
export type PageLoad<
19+
OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>
20+
> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
1321
export type PageLoadEvent = Parameters<PageLoad>[0];
14-
export type PageData = Expand<Omit<PageParentData, keyof Kit.AwaitedProperties<Awaited<ReturnType<typeof import('../../../../../../x/[[optional]]/+page.js').load>>>> & EnsureDefined<Kit.AwaitedProperties<Awaited<ReturnType<typeof import('../../../../../../x/[[optional]]/+page.js').load>>>>>;
22+
export type PageData = Expand<
23+
Omit<
24+
PageParentData,
25+
keyof Kit.AwaitedProperties<
26+
Awaited<
27+
ReturnType<typeof import('../../../../../../../../../../x/[[optional]]/+page.js').load>
28+
>
29+
>
30+
> &
31+
EnsureDefined<
32+
Kit.AwaitedProperties<
33+
Awaited<
34+
ReturnType<typeof import('../../../../../../../../../../x/[[optional]]/+page.js').load>
35+
>
36+
>
37+
>
38+
>;

0 commit comments

Comments
 (0)