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

array() and tuple() only define mutable Arrays, not ReadonlyArray #736

Open
jakubwolny opened this issue Apr 1, 2021 · 3 comments
Open
Labels
feature ♥ help please Issues that we'd love help solving

Comments

@jakubwolny
Copy link
Contributor

I guess I have similar problem to #523

I wanted to define a property inside the object which is ReadonlyArray, so I did:
tags: array(string())

but then when I tried to use the that object in other places of the app TS complained, since ReadonlyArray is a subset of Array (does not implement mutation methods).

Is there a way to do that using refine (since it's a generic) or is it necessary to update superstruct code?

btw. I'm sorry if the question is stupid, I'm quite new to the library - but so far I'm in love with it! Definitely the best runtime TS checker I've seen!

jakubwolny added a commit to jakubwolny/superstruct that referenced this issue Jun 30, 2021
@jakubwolny jakubwolny changed the title array() only defines mutable Array, not ReadonlyArray array() and tuple() only define mutable Arrays, not ReadonlyArray Jul 3, 2021
@ianstormtaylor
Copy link
Owner

Sorry for the slow reply here @jakubwolny. Interesting use case. I wonder if it makes sense to have a readonly() struct that would just update the typings to know that it's readonly? (Other than that it's an identity struct.)

@jakubwolny
Copy link
Contributor Author

@ianstormtaylor that's an awesome idea! for array() it could change the type from Array to ReadonlyArray, and for object() it could add "readonly" modifier for object properties.

My use case is pretty common I guess - I read some data from the API, validate the type, and display it. So I want to make sure that there's no mutation happening somewhere deep down.

@shellscape
Copy link

tossing a vote in for readonly(). we've got the use case of a readonly object.

@ianstormtaylor ianstormtaylor added feature ♥ help please Issues that we'd love help solving labels Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ♥ help please Issues that we'd love help solving
Projects
None yet
Development

No branches or pull requests

3 participants