Skip to content

Commit

Permalink
Add strong types for JSON.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
sstur committed Feb 20, 2023
1 parent b21978d commit 67fff97
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"import": "./dist/json-parse.mjs",
"default": "./dist/json-parse.js"
},
"./json-stringify": {
"types": "./dist/json-stringify.d.ts",
"import": "./dist/json-stringify.mjs",
"default": "./dist/json-stringify.js"
},
"./fetch": {
"types": "./dist/fetch.d.ts",
"import": "./dist/fetch.mjs",
Expand Down
24 changes: 24 additions & 0 deletions src/entrypoints/json-stringify.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
interface JSON {
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify<T = any>(
value: T,
replacer?: (this: any, key: string, value: any) => any,
space?: string | number
): undefined extends T ? string | undefined : string;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify<T = any>(
value: T,
replacer?: (number | string)[] | null,
space?: string | number
): undefined extends T ? string | undefined : string;
}
27 changes: 27 additions & 0 deletions src/tests/json-stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(() => {
const result = JSON.stringify(undefined);
type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const result = JSON.stringify(5 as undefined | number);
type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const result = JSON.stringify(undefined as any);
type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const result = JSON.stringify(undefined as unknown);
type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const value: null | string | number | boolean | object | Array<any> = null;
const result = JSON.stringify(value);
type tests = [Expect<Equal<typeof result, string>>];
});

0 comments on commit 67fff97

Please sign in to comment.