Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct types for JSON.stringify #190

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -44,6 +44,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
36 changes: 36 additions & 0 deletions src/entrypoints/json-stringify.d.ts
@@ -0,0 +1,36 @@
interface JSON {
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string or undefined.
* @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>(
value: T,
replacer?: (this: any, key: string, value: any) => any,
space?: string | number,
): T extends {} | null
? T extends (..._: any) => void
? undefined
: string
: T extends undefined
? undefined
: string | undefined;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string or undefined.
* @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>(
value: T,
replacer?: (number | string)[] | null,
space?: string | number,
): T extends {} | null
? T extends (..._: any) => void
? undefined
: string
: T extends undefined
? undefined
: string | undefined;
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Expand Up @@ -2,6 +2,7 @@
/// <reference path="filter-boolean.d.ts" />
/// <reference path="is-array.d.ts" />
/// <reference path="json-parse.d.ts" />
/// <reference path="json-stringify.d.ts" />
/// <reference path="array-includes.d.ts" />
/// <reference path="set-has.d.ts" />
/// <reference path="map-has.d.ts" />
Expand Down
63 changes: 63 additions & 0 deletions src/tests/json-stringify.ts
@@ -0,0 +1,63 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(() => {
const result = JSON.stringify({});

type tests = [Expect<Equal<typeof result, string>>];
});

doNotExecute(() => {
const result = JSON.stringify(null);

type tests = [Expect<Equal<typeof result, string>>];
});

doNotExecute(() => {
const result = JSON.stringify(undefined);

type tests = [Expect<Equal<typeof result, undefined>>];
});

doNotExecute(() => {
// create a something that is either an object or null
let toBeStringified: {} | null = Math.random() > 0.5 ? {} : null;
const result = JSON.stringify(toBeStringified);

type tests = [Expect<Equal<typeof result, string>>];
});

doNotExecute(() => {
// create a something that is either an object or undefined
let toBeStringified: {} | undefined = Math.random() > 0.5 ? {} : undefined;
const result = JSON.stringify(toBeStringified);

type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
// create a something that is a function
const result = JSON.stringify(function () {});

type tests = [Expect<Equal<typeof result, undefined>>];
});

doNotExecute(() => {
// create a something that is a function
const result = JSON.stringify(function (hello: any, world: any) {});

type tests = [Expect<Equal<typeof result, undefined>>];
});

doNotExecute(() => {
// create a something that is of type any
let toBeStringified: any;
const result = JSON.stringify(toBeStringified);

type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const result = JSON.stringify("undefined");

type tests = [Expect<Equal<typeof result, string>>];
});