-
Notifications
You must be signed in to change notification settings - Fork 127
✨ Add strong typing for JSON.stringify
#124
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
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR builds on top of #123. Here's a comparison of the two branches.
Related pull requests
replacerfunction in this PR has a strong type, i.e. noanyorunknown.toJSONmethod correctly, as well as constructor functions and symbols.How do I use the
replacerfunction?The biggest change made in this PR is providing a sound type for
JSON.stringifywhen thereplacerfunction is provided. You can now use generics to specify what the input and return types of thereplacerfunction should be.Best Practice: Always provide the generic type to
JSON.stringifyif you're using thereplacerfunction. If you don't do so, then TypeScript will use the incorrect type definition ofJSON.stringifyfrom the built-in library.So, how do you use the new and improved
replacerfunction? Consider the following example.There are a lot of things going on here.
We provided the input generic type
Date | Map<string, Date>instead of justMap<string, Date>. This is because thereplacerprocesses the input value recursively. Hence, the first time when thereplaceris called withMap<string, Date>, it converts the input map into an object. Subsequently, thereplaceris called on the values of that object which are of typeDate. Hence, we need to provide the input generic typeDate | Map<string, Date>.If we provide just
Map<string, Date>as the input generic type then we'll get an error.Notice that even though the input generic type is
Date | Map<string, Date>, yet the type ofvalueinside thereplacerfunction isstring | Map<string, Date>. This is becauseDateobjects in JavaScript have atoJSONproperty which returns astring, andJSON.stringifycalls thetoJSONmethod to change theDateto astringbefore calling thereplacerfunction.The new and improved type definitions for
JSON.stringifyare aware of this fact and provide the correct type.Notice that the return type is just
stringinstead ofstring | undefined. This is because thereplacerfunction in the above example never returnsundefined. Hence,JSON.stringifycan never returnundefined.On the flip side, we could return
undefinedfrom thereplacerfunction to filter out certain values. However, if we do so then the return type would bestring | undefined. Consider the following example.Conclusion
As you can see, we can give very strong type definitions for
JSON.stringifywithout compromising on type-safety and soundness.