@sveltejs/kit 1.0.0-next.441 → 1.0.0-next.442
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/core/sync/write_types/index.js +12 -9
- package/src/core/sync/write_types/test/layout-advanced/_expected/(main)/sub/$types.d.ts +5 -4
- package/src/core/sync/write_types/test/slugs-layout-not-all-pages-have-load/_expected/nested/[...rest]/$types.d.ts +5 -4
- package/src/runtime/server/index.js +5 -2
package/package.json
CHANGED
|
@@ -167,15 +167,20 @@ function update_types(config, routes, route) {
|
|
|
167
167
|
`type RouteParams = { ${route.names.map((param) => `${param}: string`).join('; ')} }`
|
|
168
168
|
);
|
|
169
169
|
|
|
170
|
+
// 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
|
|
170
171
|
if (route.layout || route.leaf) {
|
|
171
|
-
//
|
|
172
|
+
// If T extends the empty object, void is also allowed as a return type
|
|
172
173
|
declarations.push(`type MaybeWithVoid<T> = {} extends T ? T | void : T;`);
|
|
174
|
+
// Returns the key of the object whose values are required.
|
|
173
175
|
declarations.push(
|
|
174
176
|
`export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K; }[keyof T];`
|
|
175
177
|
);
|
|
178
|
+
// Helper type to get the correct output type for load functions. It should be passed the parent type to check what types from App.PageData are still required.
|
|
179
|
+
// If none, void is also allowed as a return type.
|
|
176
180
|
declarations.push(
|
|
177
181
|
`type OutputDataShape<T> = MaybeWithVoid<Omit<App.PageData, RequiredKeys<T>> & Partial<Pick<App.PageData, keyof T & keyof App.PageData>> & Record<string, any>>`
|
|
178
182
|
);
|
|
183
|
+
// null & {} == null, we need to prevent that in some situations
|
|
179
184
|
declarations.push(`type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;`);
|
|
180
185
|
}
|
|
181
186
|
|
|
@@ -277,9 +282,7 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
|
|
|
277
282
|
|
|
278
283
|
const parent_type = `${prefix}ServerParentData`;
|
|
279
284
|
|
|
280
|
-
declarations.push(
|
|
281
|
-
`type ${parent_type} = EnsureParentData<${get_parent_type(node, 'LayoutServerData')}>;`
|
|
282
|
-
);
|
|
285
|
+
declarations.push(`type ${parent_type} = ${get_parent_type(node, 'LayoutServerData')};`);
|
|
283
286
|
|
|
284
287
|
// +page.js load present -> server can return all-optional data
|
|
285
288
|
const output_data_shape =
|
|
@@ -317,9 +320,7 @@ function process_node(node, outdir, is_page, all_pages_have_load = true) {
|
|
|
317
320
|
exports.push(`export type ${prefix}ServerData = ${server_data};`);
|
|
318
321
|
|
|
319
322
|
const parent_type = `${prefix}ParentData`;
|
|
320
|
-
declarations.push(
|
|
321
|
-
`type ${parent_type} = EnsureParentData<${get_parent_type(node, 'LayoutData')}>;`
|
|
322
|
-
);
|
|
323
|
+
declarations.push(`type ${parent_type} = ${get_parent_type(node, 'LayoutData')};`);
|
|
323
324
|
|
|
324
325
|
if (node.shared) {
|
|
325
326
|
const content = fs.readFileSync(node.shared, 'utf8');
|
|
@@ -394,12 +395,14 @@ function get_parent_type(node, type) {
|
|
|
394
395
|
parent = parent.parent;
|
|
395
396
|
}
|
|
396
397
|
|
|
397
|
-
let parent_str = parent_imports[0] || '{}'
|
|
398
|
+
let parent_str = `EnsureParentData<${parent_imports[0] || '{}'}>`;
|
|
398
399
|
for (let i = 1; i < parent_imports.length; i++) {
|
|
399
400
|
// Omit is necessary because a parent could have a property with the same key which would
|
|
400
401
|
// cause a type conflict. At runtime the child overwrites the parent property in this case,
|
|
401
402
|
// so reflect that in the type definition.
|
|
402
|
-
|
|
403
|
+
// EnsureParentData is necessary because {something: string} & null becomes null.
|
|
404
|
+
// Output types of server loads can be null but when passed in through the `parent` parameter they are the empty object instead.
|
|
405
|
+
parent_str = `Omit<${parent_str}, keyof ${parent_imports[i]}> & EnsureParentData<${parent_imports[i]}>`;
|
|
403
406
|
}
|
|
404
407
|
return parent_str;
|
|
405
408
|
}
|
|
@@ -11,10 +11,11 @@ type OutputDataShape<T> = MaybeWithVoid<
|
|
|
11
11
|
Record<string, any>
|
|
12
12
|
>;
|
|
13
13
|
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
|
|
14
|
-
type PageParentData =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
type PageParentData = Omit<
|
|
15
|
+
EnsureParentData<import('../../$types.js').LayoutData>,
|
|
16
|
+
keyof import('../$types.js').LayoutData
|
|
17
|
+
> &
|
|
18
|
+
EnsureParentData<import('../$types.js').LayoutData>;
|
|
18
19
|
|
|
19
20
|
export type PageServerData = null;
|
|
20
21
|
export type PageLoad<
|
|
@@ -11,10 +11,11 @@ type OutputDataShape<T> = MaybeWithVoid<
|
|
|
11
11
|
Record<string, any>
|
|
12
12
|
>;
|
|
13
13
|
type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;
|
|
14
|
-
type PageParentData =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
type PageParentData = Omit<
|
|
15
|
+
EnsureParentData<import('../../$types.js').LayoutData>,
|
|
16
|
+
keyof import('../$types.js').LayoutData
|
|
17
|
+
> &
|
|
18
|
+
EnsureParentData<import('../$types.js').LayoutData>;
|
|
18
19
|
|
|
19
20
|
export type PageServerData = null;
|
|
20
21
|
export type PageLoad<
|
|
@@ -280,10 +280,13 @@ export async function respond(request, options, state) {
|
|
|
280
280
|
/** @type {Record<string, any>} */
|
|
281
281
|
const data = {};
|
|
282
282
|
for (let j = 0; j < i; j += 1) {
|
|
283
|
-
const parent = /** @type {import('types').ServerDataNode} */ (
|
|
283
|
+
const parent = /** @type {import('types').ServerDataNode | null} */ (
|
|
284
284
|
await functions[j]()
|
|
285
285
|
);
|
|
286
|
-
|
|
286
|
+
|
|
287
|
+
if (parent) {
|
|
288
|
+
Object.assign(data, parent.data);
|
|
289
|
+
}
|
|
287
290
|
}
|
|
288
291
|
return data;
|
|
289
292
|
}
|