-
Notifications
You must be signed in to change notification settings - Fork 125
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
✨ Add strong typing for JSON.stringify
#124
Closed
Closed
Conversation
This file contains 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
replacer
function in this PR has a strong type, i.e. noany
orunknown
.toJSON
method correctly, as well as constructor functions and symbols.How do I use the
replacer
function?The biggest change made in this PR is providing a sound type for
JSON.stringify
when thereplacer
function is provided. You can now use generics to specify what the input and return types of thereplacer
function should be.Best Practice: Always provide the generic type to
JSON.stringify
if you're using thereplacer
function. If you don't do so, then TypeScript will use the incorrect type definition ofJSON.stringify
from the built-in library.So, how do you use the new and improved
replacer
function? 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 thereplacer
processes the input value recursively. Hence, the first time when thereplacer
is called withMap<string, Date>
, it converts the input map into an object. Subsequently, thereplacer
is 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 ofvalue
inside thereplacer
function isstring | Map<string, Date>
. This is becauseDate
objects in JavaScript have atoJSON
property which returns astring
, andJSON.stringify
calls thetoJSON
method to change theDate
to astring
before calling thereplacer
function.The new and improved type definitions for
JSON.stringify
are aware of this fact and provide the correct type.Notice that the return type is just
string
instead ofstring | undefined
. This is because thereplacer
function in the above example never returnsundefined
. Hence,JSON.stringify
can never returnundefined
.On the flip side, we could return
undefined
from thereplacer
function 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.stringify
without compromising on type-safety and soundness.