diff --git a/.changeset/purple-readers-build.md b/.changeset/purple-readers-build.md new file mode 100644 index 000000000000..762698421322 --- /dev/null +++ b/.changeset/purple-readers-build.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +`RequestHandlerOutput` accepts body when it has or maybe is `undefined` diff --git a/packages/kit/test/typings/endpoint.test.ts b/packages/kit/test/typings/endpoint.test.ts index f805a811d4a4..9f5af8ec3fc5 100644 --- a/packages/kit/test/typings/endpoint.test.ts +++ b/packages/kit/test/typings/endpoint.test.ts @@ -5,10 +5,11 @@ const valid_body = { num: 12345, bool: true, null: null, + maybe: Math.random() < 0.5 ? undefined : true, custom: { toJSON: () => 'custom toJSON function' }, - list: ['string', 12345, false, null], + list: ['string', 12345, false, null, undefined], nested: { another: 'string', big_num: 98765, @@ -84,13 +85,6 @@ export const differential_headers_assignment: RequestHandler = () => { // --- invalid cases --- -// @ts-expect-error - should not have undefined (should it not?) -export const error_no_undefined: RequestHandler = () => { - return { - body: { no: Math.random() < 0.5 ? undefined : 'something' } - }; -}; - // @ts-expect-error - body must be JSON serializable export const error_body_must_be_serializable: RequestHandler = () => { return { diff --git a/packages/kit/types/private.d.ts b/packages/kit/types/private.d.ts index d51d7864e645..33b8d487f272 100644 --- a/packages/kit/types/private.d.ts +++ b/packages/kit/types/private.d.ts @@ -208,7 +208,15 @@ export type HttpMethod = 'get' | 'head' | 'post' | 'put' | 'delete' | 'patch'; export type JSONObject = { [key: string]: JSONValue }; -export type JSONValue = string | number | boolean | null | ToJSON | JSONValue[] | JSONObject; +export type JSONValue = + | string + | number + | boolean + | null + | undefined + | ToJSON + | JSONValue[] + | JSONObject; export interface LoadInput> { url: URL;