-
Notifications
You must be signed in to change notification settings - Fork 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
💥 Add strong typing for JSON.parse
when reviver
is specified
#123
Closed
aaditmshah
wants to merge
17
commits into
mattpocock:main
from
aaditmshah:feature/json-parse-reviver
Closed
💥 Add strong typing for JSON.parse
when reviver
is specified
#123
aaditmshah
wants to merge
17
commits into
mattpocock:main
from
aaditmshah:feature/json-parse-reviver
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
This was referenced Mar 25, 2023
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 #121. Here's a comparison of the two branches.
In response to “Rules we won't add
“Generics for
JSON.parse
,Response.json
etc”The primary concern is using generics to provide the return type of
JSON.parse
without providing areviver
argument. This is unsound, as you can see in the following code snippet.This PR does not allow that. Following are the changes that I made to the type definition of
JSON.parse
.And here are the type definitions of the utility types,
JsonValue
,JsonValueF
,JsonHolder
, etc.A sound and type-safe way to use
JSON.parse
The new
JSON.parse
type definitions introduced in this PR are both type-safe and sound. For example, the following code snippet is invalid.You can provide the return type of
JSON.parse
if and only if you also provide a valid argument forreviver
. And, thereviver
function has a sound type which constraints the return type that you can provide toJSON.parse
.This ensures that you never get an unsound result type when using
JSON.parse
. For example, if you only provide one argument toJSON.parse
, i.e. thetext
that you want to parse, then the return type of the function is alwaysJsonValue
.If you want, you can provide the return type of
JSON.parse
along with areviver
to get a different result.It's type-safe and sound because the return type provided for
JSON.parse
has to match both the input type and the return type of thereviver
. Hence, you can't cheat.I firmly believe that you'd really have to go out of your way to break the type-safety provided by the type definitions in this PR.
What does the
reviver
do?At its core,
JSON.parse
with areviver
is simply a structural fold, a.k.a. a catamorphism. It takes an initial F-algebra, i.e. thereviver
, which describes how to convert values of typeJsonValueF<A>
into values of typeA
, and uses this description to convert anyJsonValue
into a value of typeA
.This pure mathematical logic is muddied a little bit by the implementation of
JSON.parse
with respect to thereviver
. For example, if thereviver
returnsundefined
then the corresponding object property or array element is deleted. This could lead to sparse arrays. However, since the return type is generic, we don't need to change the type of theJSON.parse
function. Garbage in, garbage out. If yourreviver
function can returnundefined
, then don't be surprised if you seeundefined
in the result.Finally, the
this
context of thereviver
is almost impossible to work with in a type-safe way. Hence, I gave it the very conservative typeJsonHolder
whereJsonHolder<K extends string, A> = Record<K, JsonValueF<A>>
. Essentially, if you have a reviver with the inputsthis
,key
, andvalue
, then the type ofthis[key]
is the same as the type ofvalue
. The other properties, or array elements, ofthis
could either be of the typeJsonValue
or of the return typeA
, depending upon whether or not they have been processed by thereviver
. However, there's no safe way to access these properties or array elements. The best way to access them would be to useObject.entries
orObject.values
which return values of typeunknown
. Hence, there's no good reason to givethis
a more specific type to access the other object properties or array elements.Conclusion
Hopefully, I've convinced you that it's indeed possible to provide a safe and sound type definition for
JSON.parse
. And there are many advantages in doing so. The result type ofJSON.parse
is no longerunknown
, unless you use areviver
but forget to provide the return type. In addition, thereviver
function provides more type-safety too. No moreany
types anywhere.