Skip to content

JSON.stringify with optional function-style replacer #58

Closed
@danielrentz

Description

@danielrentz

In our project, we have a code place that wants to call JSON.stringify with an optional replacer function contained in a variable. This causes a type error now. Optional replacer still works with array type though.

const rnd = Math.random() > 0.5;
const replacer1 = rnd ? [""] : undefined;
const replacer2 = rnd ? (() => "") : undefined;

JSON.stringify({}, replacer1, 2);    // still works
JSON.stringify({}, replacer2, 2);    // used to work, now a type error

I think the "replacer" arg should be made optional in all three signatures. Calling the function explicitly with undefined (as in JSON.stringify({}, undefined, 2)) will still pick the first match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions