-
-
Notifications
You must be signed in to change notification settings - Fork 506
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
Proposal: Jsonifyable #356
Comments
There are a lot more values that are "jsonifiable" though. How do you think we should handle those? For example, |
And I think it should be |
@sindresorhus Thank you for the feedback. After playing with it a bit more, I think I misunderstood my own use case. Closing. For anyone who finds their way here, it should be noted that JSON.stringify will only fail if an object has cycles. Unless there is a way in Typescript to detect object cycles, anything is Jsonifiable :) |
|
I can see the usefulness of also supporting |
I agree that a But how about a @sindresorhus I can make a PR. edit: I've just stumbled upon sindresorhus/serialize-error#55 which I think could benefit of this type. |
Does my suggestion above cover this idea here? Or do you think only additionally allowing |
@papb For example, I want to to disallow any values that would lead to an incompatible type after deserialization so I can be sure that what comes out the other end of the wire is type equivalent. Just for clarity, I think the existing |
Array types like In some sense I think PartialDeep is going a little too far. |
👍
I think we could add an optional type parameter option for that: |
@sindresorhus So are you OK with me doing a PR as I proposed above? Also can you please answer this? |
@papb Consider also whether |
@benallfree Ah, yes, I think I agree about |
@papb Oh gotcha, yes I’d love to see Jsonifiable, I think that would be an excellent addition. |
@papb If you do a PR please also consider the case of allowing incompatible types which are coerced to undefined as long as the type allows it. See my comment above #356 (comment) |
Yes
Yes |
@papb Another use case just came up in remix-run/remix#2201 |
I think it would be nice if we had utility type type User = {
name: string,
parent: User | null,
sayHello: () => string,
friends: Map<string, User>,
birthDate: Date,
}
const user = JSON.parse(/* serialized user */) as ParsedJson<User>
// typeof user
{
name: string,
parent: ParsedJson<User> | null,
sayHello: never,
friends: {},
birthDate: string,
} |
I'm hitting a problem where
(string | undefined)[]
is incompatible withJsonValue
.https://codesandbox.io/s/nice-water-lp2n5
Will produce:
I think this error is correct because
undefined
is certainly not allowed in a JSON value.What I want is a way to express an object that can pass JSON.stringify without error. Ie, Jsonifyable!
It's almost identical, but
undefined
is allowed as a value:If you'd like me to turn this into a PR, I'd be happy to. I'm using it in my production code right now.
The text was updated successfully, but these errors were encountered: