Skip to content
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

Typescript return string or undefined? #10

Open
dylanqian29 opened this issue Apr 20, 2022 · 4 comments
Open

Typescript return string or undefined? #10

dylanqian29 opened this issue Apr 20, 2022 · 4 comments

Comments

@dylanqian29
Copy link

dylanqian29 commented Apr 20, 2022

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! :)

@aren55555
Copy link

We'd be happy to contribute a fix PR, but just wanted to get your thoughts on this in case we were missing anything!

@erdtman
Copy link
Owner

erdtman commented Apr 21, 2022

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?

@aren55555
Copy link

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.

@cyberphone
Copy link

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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants