Skip to content

Commit 5e75c9c

Browse files
authored
[feat] improve readability of generated types (#6883)
1 parent 434df79 commit 5e75c9c

File tree

16 files changed

+208
-152
lines changed

16 files changed

+208
-152
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ function update_types(config, routes, route) {
159159
/** @type {string[]} */
160160
const exports = [];
161161

162+
// add 'Expand' helper
163+
// Makes sure a type is "repackaged" and therefore more readable
164+
declarations.push('type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;');
162165
declarations.push(
163166
`type RouteParams = { ${route.names.map((param) => `${param}: string`).join('; ')} }`
164167
);
@@ -278,7 +281,7 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
278281
written_proxies.push(`proxy${basename}`);
279282
}
280283

281-
server_data = get_data_type(node.server, 'null', proxy);
284+
server_data = get_data_type(node.server, 'null', proxy, true);
282285

283286
const parent_type = `${prefix}ServerParentData`;
284287

@@ -305,7 +308,7 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
305308
? `./proxy${replace_ext_with_js(basename)}`
306309
: path_to_original(outdir, node.server);
307310

308-
type = `Kit.AwaitedActions<typeof import('${from}').actions>`;
311+
type = `Expand<Kit.AwaitedActions<typeof import('${from}').actions>>`;
309312
}
310313
}
311314
exports.push(`export type ActionData = ${type};`);
@@ -328,7 +331,7 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
328331

329332
const type = get_data_type(node.shared, `${parent_type} & ${prefix}ServerData`, proxy);
330333

331-
data = `Omit<${parent_type}, keyof ${type}> & ${type}`;
334+
data = `Expand<Omit<${parent_type}, keyof ${type}> & ${type}>`;
332335

333336
const output_data_shape =
334337
!is_page && all_pages_have_load
@@ -340,9 +343,9 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
340343

341344
exports.push(`export type ${prefix}LoadEvent = Parameters<${prefix}Load>[0];`);
342345
} else if (server_data === 'null') {
343-
data = parent_type;
346+
data = `Expand<${parent_type}>`;
344347
} else {
345-
data = `Omit<${parent_type}, keyof ${prefix}ServerData> & ${prefix}ServerData`;
348+
data = `Expand<Omit<${parent_type}, keyof ${prefix}ServerData> & ${prefix}ServerData>`;
346349
}
347350

348351
exports.push(`export type ${prefix}Data = ${data};`);
@@ -353,16 +356,18 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
353356
* @param {string} file_path
354357
* @param {string} fallback
355358
* @param {Proxy} proxy
359+
* @param {boolean} expand
356360
*/
357-
function get_data_type(file_path, fallback, proxy) {
361+
function get_data_type(file_path, fallback, proxy, expand = false) {
358362
if (proxy) {
359363
if (proxy.exports.includes('load')) {
360364
// If the file wasn't tweaked, we can use the return type of the original file.
361365
// The advantage is that type updates are reflected without saving.
362366
const from = proxy.modified
363367
? `./proxy${replace_ext_with_js(path.basename(file_path))}`
364368
: path_to_original(outdir, file_path);
365-
return `Kit.AwaitedProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
369+
const type = `Kit.AwaitedProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
370+
return expand ? `Expand<${type}>` : type;
366371
} else {
367372
return fallback;
368373
}

packages/kit/src/core/sync/write_types/test/layout-advanced/_expected/$types.d.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -21,12 +22,14 @@ export type LayoutLoad<
2122
| void
2223
> = Kit.Load<LayoutParams, LayoutServerData, LayoutParentData, OutputData>;
2324
export type LayoutLoadEvent = Parameters<LayoutLoad>[0];
24-
export type LayoutData = Omit<
25-
LayoutParentData,
26-
keyof Kit.AwaitedProperties<
27-
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
28-
>
29-
> &
30-
Kit.AwaitedProperties<
31-
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
32-
>;
25+
export type LayoutData = Expand<
26+
Omit<
27+
LayoutParentData,
28+
keyof Kit.AwaitedProperties<
29+
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
30+
>
31+
> &
32+
Kit.AwaitedProperties<
33+
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
34+
>
35+
>;

packages/kit/src/core/sync/write_types/test/layout-advanced/_expected/(main)/$types.d.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -21,22 +22,26 @@ export type PageLoad<
2122
OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>
2223
> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
2324
export type PageLoadEvent = Parameters<PageLoad>[0];
24-
export type PageData = Omit<
25-
PageParentData,
26-
keyof Kit.AwaitedProperties<
27-
Awaited<ReturnType<typeof import('../../../../../../../../../(main)/+page.js').load>>
28-
>
29-
> &
30-
Kit.AwaitedProperties<
31-
Awaited<ReturnType<typeof import('../../../../../../../../../(main)/+page.js').load>>
32-
>;
25+
export type PageData = Expand<
26+
Omit<
27+
PageParentData,
28+
keyof Kit.AwaitedProperties<
29+
Awaited<ReturnType<typeof import('../../../../../../../../../(main)/+page.js').load>>
30+
>
31+
> &
32+
Kit.AwaitedProperties<
33+
Awaited<ReturnType<typeof import('../../../../../../../../../(main)/+page.js').load>>
34+
>
35+
>;
3336
export type LayoutServerLoad<
3437
OutputData extends (Partial<App.PageData> & Record<string, any>) | void =
3538
| (Partial<App.PageData> & Record<string, any>)
3639
| void
3740
> = Kit.ServerLoad<LayoutParams, LayoutServerParentData, OutputData>;
3841
export type LayoutServerLoadEvent = Parameters<LayoutServerLoad>[0];
39-
export type LayoutServerData = Kit.AwaitedProperties<
40-
Awaited<ReturnType<typeof import('../../../../../../../../../(main)/+layout.server.js').load>>
42+
export type LayoutServerData = Expand<
43+
Kit.AwaitedProperties<
44+
Awaited<ReturnType<typeof import('../../../../../../../../../(main)/+layout.server.js').load>>
45+
>
4146
>;
42-
export type LayoutData = Omit<LayoutParentData, keyof LayoutServerData> & LayoutServerData;
47+
export type LayoutData = Expand<Omit<LayoutParentData, keyof LayoutServerData> & LayoutServerData>;

packages/kit/src/core/sync/write_types/test/layout-advanced/_expected/(main)/sub/$types.d.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -22,12 +23,14 @@ export type PageLoad<
2223
OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>
2324
> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
2425
export type PageLoadEvent = Parameters<PageLoad>[0];
25-
export type PageData = Omit<
26-
PageParentData,
27-
keyof Kit.AwaitedProperties<
28-
Awaited<ReturnType<typeof import('../../../../../../../../../../(main)/sub/+page.js').load>>
29-
>
30-
> &
31-
Kit.AwaitedProperties<
32-
Awaited<ReturnType<typeof import('../../../../../../../../../../(main)/sub/+page.js').load>>
33-
>;
26+
export type PageData = Expand<
27+
Omit<
28+
PageParentData,
29+
keyof Kit.AwaitedProperties<
30+
Awaited<ReturnType<typeof import('../../../../../../../../../../(main)/sub/+page.js').load>>
31+
>
32+
> &
33+
Kit.AwaitedProperties<
34+
Awaited<ReturnType<typeof import('../../../../../../../../../../(main)/sub/+page.js').load>>
35+
>
36+
>;
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -24,22 +25,26 @@ export type PageServerLoad<
2425
> = Kit.ServerLoad<RouteParams, PageServerParentData, OutputData>;
2526
export type PageServerLoadEvent = Parameters<PageServerLoad>[0];
2627
export type ActionData = unknown;
27-
export type PageServerData = Kit.AwaitedProperties<
28-
Awaited<ReturnType<typeof import('../../../../../../../../+page.server.js').load>>
28+
export type PageServerData = Expand<
29+
Kit.AwaitedProperties<
30+
Awaited<ReturnType<typeof import('../../../../../../../../+page.server.js').load>>
31+
>
2932
>;
3033
export type PageLoad<
3134
OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>
3235
> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
3336
export type PageLoadEvent = Parameters<PageLoad>[0];
34-
export type PageData = Omit<
35-
PageParentData,
36-
keyof Kit.AwaitedProperties<
37-
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
38-
>
39-
> &
40-
Kit.AwaitedProperties<
41-
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
42-
>;
37+
export type PageData = Expand<
38+
Omit<
39+
PageParentData,
40+
keyof Kit.AwaitedProperties<
41+
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
42+
>
43+
> &
44+
Kit.AwaitedProperties<
45+
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
46+
>
47+
>;
4348
export type Action = Kit.Action<RouteParams>;
4449
export type Actions = Kit.Actions<RouteParams>;
4550
export type LayoutServerLoad<
@@ -48,22 +53,26 @@ export type LayoutServerLoad<
4853
| void
4954
> = Kit.ServerLoad<LayoutParams, LayoutServerParentData, OutputData>;
5055
export type LayoutServerLoadEvent = Parameters<LayoutServerLoad>[0];
51-
export type LayoutServerData = Kit.AwaitedProperties<
52-
Awaited<ReturnType<typeof import('../../../../../../../../+layout.server.js').load>>
56+
export type LayoutServerData = Expand<
57+
Kit.AwaitedProperties<
58+
Awaited<ReturnType<typeof import('../../../../../../../../+layout.server.js').load>>
59+
>
5360
>;
5461
export type LayoutLoad<
5562
OutputData extends (Partial<App.PageData> & Record<string, any>) | void =
5663
| (Partial<App.PageData> & Record<string, any>)
5764
| void
5865
> = Kit.Load<LayoutParams, LayoutServerData, LayoutParentData, OutputData>;
5966
export type LayoutLoadEvent = Parameters<LayoutLoad>[0];
60-
export type LayoutData = Omit<
61-
LayoutParentData,
62-
keyof Kit.AwaitedProperties<
63-
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
64-
>
65-
> &
66-
Kit.AwaitedProperties<
67-
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
68-
>;
67+
export type LayoutData = Expand<
68+
Omit<
69+
LayoutParentData,
70+
keyof Kit.AwaitedProperties<
71+
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
72+
>
73+
> &
74+
Kit.AwaitedProperties<
75+
Awaited<ReturnType<typeof import('../../../../../../../../+layout.js').load>>
76+
>
77+
>;
6978
export type RequestEvent = Kit.RequestEvent<RouteParams>;

packages/kit/src/core/sync/write_types/test/simple-page-server-and-shared/_expected/$types.d.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -23,24 +24,28 @@ export type PageServerLoad<
2324
> = Kit.ServerLoad<RouteParams, PageServerParentData, OutputData>;
2425
export type PageServerLoadEvent = Parameters<PageServerLoad>[0];
2526
export type ActionData = unknown;
26-
export type PageServerData = Kit.AwaitedProperties<
27-
Awaited<ReturnType<typeof import('../../../../../../../../+page.server.js').load>>
27+
export type PageServerData = Expand<
28+
Kit.AwaitedProperties<
29+
Awaited<ReturnType<typeof import('../../../../../../../../+page.server.js').load>>
30+
>
2831
>;
2932
export type PageLoad<
3033
OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>
3134
> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
3235
export type PageLoadEvent = Parameters<PageLoad>[0];
33-
export type PageData = Omit<
34-
PageParentData,
35-
keyof Kit.AwaitedProperties<
36-
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
37-
>
38-
> &
39-
Kit.AwaitedProperties<
40-
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
41-
>;
36+
export type PageData = Expand<
37+
Omit<
38+
PageParentData,
39+
keyof Kit.AwaitedProperties<
40+
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
41+
>
42+
> &
43+
Kit.AwaitedProperties<
44+
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
45+
>
46+
>;
4247
export type Action = Kit.Action<RouteParams>;
4348
export type Actions = Kit.Actions<RouteParams>;
4449
export type LayoutServerData = null;
45-
export type LayoutData = LayoutParentData;
50+
export type LayoutData = Expand<LayoutParentData>;
4651
export type RequestEvent = Kit.RequestEvent<RouteParams>;

packages/kit/src/core/sync/write_types/test/simple-page-server-only/_expected/$types.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -20,15 +21,17 @@ export type PageServerLoad<
2021
OutputData extends OutputDataShape<PageServerParentData> = OutputDataShape<PageServerParentData>
2122
> = Kit.ServerLoad<RouteParams, PageServerParentData, OutputData>;
2223
export type PageServerLoadEvent = Parameters<PageServerLoad>[0];
23-
export type ActionData = Kit.AwaitedActions<
24-
typeof import('../../../../../../../../+page.server.js').actions
24+
export type ActionData = Expand<
25+
Kit.AwaitedActions<typeof import('../../../../../../../../+page.server.js').actions>
2526
>;
26-
export type PageServerData = Kit.AwaitedProperties<
27-
Awaited<ReturnType<typeof import('../../../../../../../../+page.server.js').load>>
27+
export type PageServerData = Expand<
28+
Kit.AwaitedProperties<
29+
Awaited<ReturnType<typeof import('../../../../../../../../+page.server.js').load>>
30+
>
2831
>;
29-
export type PageData = Omit<PageParentData, keyof PageServerData> & PageServerData;
32+
export type PageData = Expand<Omit<PageParentData, keyof PageServerData> & PageServerData>;
3033
export type Action = Kit.Action<RouteParams>;
3134
export type Actions = Kit.Actions<RouteParams>;
3235
export type LayoutServerData = null;
33-
export type LayoutData = LayoutParentData;
36+
export type LayoutData = Expand<LayoutParentData>;
3437
export type RequestEvent = Kit.RequestEvent<RouteParams>;

packages/kit/src/core/sync/write_types/test/simple-page-shared-only/_expected/$types.d.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as Kit from '@sveltejs/kit';
22

3+
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
34
type RouteParams = {};
45
type MaybeWithVoid<T> = {} extends T ? T | void : T;
56
export type RequiredKeys<T> = {
@@ -20,14 +21,16 @@ export type PageLoad<
2021
OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>
2122
> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
2223
export type PageLoadEvent = Parameters<PageLoad>[0];
23-
export type PageData = Omit<
24-
PageParentData,
25-
keyof Kit.AwaitedProperties<
26-
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
27-
>
28-
> &
29-
Kit.AwaitedProperties<
30-
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
31-
>;
24+
export type PageData = Expand<
25+
Omit<
26+
PageParentData,
27+
keyof Kit.AwaitedProperties<
28+
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
29+
>
30+
> &
31+
Kit.AwaitedProperties<
32+
Awaited<ReturnType<typeof import('../../../../../../../../+page.js').load>>
33+
>
34+
>;
3235
export type LayoutServerData = null;
33-
export type LayoutData = LayoutParentData;
36+
export type LayoutData = Expand<LayoutParentData>;

0 commit comments

Comments
 (0)