Skip to content

JSON.stringify with optional function-style replacer #58

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

Closed
danielrentz opened this issue Jan 10, 2025 · 0 comments · Fixed by #61
Closed

JSON.stringify with optional function-style replacer #58

danielrentz opened this issue Jan 10, 2025 · 0 comments · Fixed by #61
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@danielrentz
Copy link
Contributor

danielrentz commented Jan 10, 2025

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.

@uhyo uhyo added bug Something isn't working help wanted Extra attention is needed labels Jan 13, 2025
danielrentz added a commit to danielrentz/better-typescript-lib that referenced this issue Jan 14, 2025
@uhyo uhyo closed this as completed in #61 Jan 14, 2025
@uhyo uhyo closed this as completed in d39aba6 Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants