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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does Json.stringify return an Either? #1909

Open
davezuch opened this issue Oct 11, 2023 · 2 comments
Open

Why does Json.stringify return an Either? #1909

davezuch opened this issue Oct 11, 2023 · 2 comments

Comments

@davezuch
Copy link

馃殌 Feature request

Current Behavior

Json.stringify is currently defined as:

export const stringify = <A>(a: A): Either<unknown, string> =>

Desired Behavior

Coming from PureScript, I don't expect encoding a value as JSON to fail.

Suggested Solution

It seems to me rather than trying to encode any JS value as JSON, most use cases (and I could be wrong here) will be trying to encode a value that extends Json.Json. If that's the case, stringify could pretty safely be implemented as:

const stringify = <Value extends Json>(value: Value): string => JSON.stringify(value)

Of course I could be overlooking an obvious reason that the current implementation makes more sense. But if the current implementation is necessary for some cases, while the implementation suggested here would work for most, then I think it's worth providing both.

Who does this impact? Who is this for?

Maybe just me but I assume this would benefit all users as they could avoid having to deal with failure when encoding JSON.

Your environment

Software Version(s)
fp-ts 2.16.1
TypeScript 5.1.6
@DenisFrezzato
Copy link
Collaborator

JSON.stringify throws a TypeError when the value contains a circular reference or a BigInt.

@davezuch
Copy link
Author

JSON.stringify throws a TypeError when the value contains a circular reference or a BigInt.

BigInt doesn't extend Json though. And while I'm still learning TS, I can't figure out how to construct a self-referencing Json type without using any then casting it to Json with stringify(notJson as Json), which if you're doing, then you should expect type-errors anyway.

So without deliberately circumventing the compiler, you shouldn't be able to call the stringify function I suggested with either.

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

2 participants