Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

array() of blocks() are readonly but PortableText requires a mutable value. #156

Closed
miklschmidt opened this issue Sep 2, 2022 · 5 comments · Fixed by #157
Closed

array() of blocks() are readonly but PortableText requires a mutable value. #156

miklschmidt opened this issue Sep 2, 2022 · 5 comments · Fixed by #157
Labels

Comments

@miklschmidt
Copy link
Contributor

Currently you have to go through some hoops to not get errors.

type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
return (
    <PortableText value={value as DeepWriteable<typeof value>} components={} />
)

Am i doing something wrong, or could we maybe use the readOnly prop in the schema to make these mutable in the resulting types?

@saiichihashimoto
Copy link
Owner

I don't remember where in the typing it is, but having this be readonly wasn't an intentional decision, but rather a consequence of convenience. It was just easier to produce a readonly value somewhere. If you can identify it with a fix, would be happy to accept a PR.

For clarity: returning a readonly type is different than the readOnly prop, nor should they be the same. The readOnly prop is so that the sanity UI doesn't allow editing. Returning a readonly type is about immutability in your codebase, which is entirely your own decision. Considering readOnly can be a function, it's very clear these refer to entirely different things.

@miklschmidt
Copy link
Contributor Author

I realized that 5 minutes after i posted, still learning sanity, just jumped on it yesterday :)

i’ll see if i can come up with a fix :)

@miklschmidt
Copy link
Contributor Author

Just to be sure, i’m just making the array/block types mutable, or do you want to drop readonly from everything? It sort of makes sense that they’re all readonly, you’re not supposed to mutate them, it just makes it harder to pass them to other code that (maybe incorrectly) don’t expect a readonly type.

@miklschmidt
Copy link
Contributor Author

miklschmidt commented Sep 2, 2022

It's easy enough to just get rid of readonly alltogether, just remove readonly from TupleOfLength, and it's gone, everything checks out. But i'm not sure that's what you want, in which case it looks a lot more complicated, and it would prolly take me a while to figure out exactly how this thing is put together (thank you so much for doing that btw, ever since tRPC i have a hard time living without inferred end to end typesafety, lol).

@github-actions
Copy link

github-actions bot commented Sep 4, 2022

🎉 This issue has been resolved in version 1.15.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

2 participants