💥 Add strong typing for JSON.parse when reviver is specified
#123
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.jsonetc”The primary concern is using generics to provide the return type of
JSON.parsewithout providing areviverargument. 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.parseThe new
JSON.parsetype 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.parseif and only if you also provide a valid argument forreviver. And, thereviverfunction 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. thetextthat you want to parse, then the return type of the function is alwaysJsonValue.If you want, you can provide the return type of
JSON.parsealong with areviverto get a different result.It's type-safe and sound because the return type provided for
JSON.parsehas 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
reviverdo?At its core,
JSON.parsewith areviveris 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 anyJsonValueinto a value of typeA.This pure mathematical logic is muddied a little bit by the implementation of
JSON.parsewith respect to thereviver. For example, if thereviverreturnsundefinedthen 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.parsefunction. Garbage in, garbage out. If yourreviverfunction can returnundefined, then don't be surprised if you seeundefinedin the result.Finally, the
thiscontext of thereviveris almost impossible to work with in a type-safe way. Hence, I gave it the very conservative typeJsonHolderwhereJsonHolder<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, ofthiscould either be of the typeJsonValueor 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.entriesorObject.valueswhich return values of typeunknown. Hence, there's no good reason to givethisa 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.parseis no longerunknown, unless you use areviverbut forget to provide the return type. In addition, thereviverfunction provides more type-safety too. No moreanytypes anywhere.