Skip to content

Commit

Permalink
undefined as valid JSONValue type (#4152)
Browse files Browse the repository at this point in the history
* failing test

* add undefined to JSONValue

* remove now valid case

* add changeset
  • Loading branch information
ignatiusmb authored Feb 28, 2022
1 parent 0731240 commit 9d3b6c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-readers-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

`RequestHandlerOutput` accepts body when it has or maybe is `undefined`
10 changes: 2 additions & 8 deletions packages/kit/test/typings/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion packages/kit/types/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Params = Record<string, string>> {
url: URL;
Expand Down

0 comments on commit 9d3b6c4

Please sign in to comment.