Typescript return string or undefined?
dylanqian29 opened this issue · 4 comments
Hi, I have read through your package and source code, and I'm wondering in what situation the serialize
method will return undefined
. Based on the type definition that is shown in this link, it seems like the output for typescript is shown as string | undefined
.
However, when I compare this to the javascript implementation in this file, it appears this method can only return as a string.
Thank you for your contribution! :)
We'd be happy to contribute a fix PR, but just wanted to get your thoughts on this in case we were missing anything!
I did some tests and could find 2 cases where it returned undefined.
- if the input is undefined
- if the object has a toJSON method that returns undefined
Not sure what the RFC says on this topic, need to check.
@cyberphone do you know?
I wonder if there's a way to more generic TS implemenetnation for this method such that based on the input type the expected outputs change.
For example, if I passed in an object {...}
or an array [...]
the only possible output would be a string
; conversely if I pass in undefined
the only possible output is undefined
... 🤔
I guess the default JSON.stringify
is typed the same as this method so perhaps this isn't really a problem and just something we should handle in our application code.
Hi @erdtman, the RFC does the assumption that the data to be canonicalized is JSON-serializable (in a generic fashion rather than JavaScript-specific), which excludes undefined
since it is not a part of JSON. The JavaScript sample in the RFC is as we have seen before somewhat simplistic.
I don't have any opinion on things that are platform-specific but if there was a JSON.canonify()
, it would surely follow JSON.stringify()
.