Repository navigation
Refactor store to remove readonly - #790
otonashixav wants to merge 3 commits into
Conversation
Pull Request Test Coverage Report for Build 1692494664
💛 - Coveralls |
|
I have a feeling that keeping it correct (all readonly) is the way to go. It is similar in practice to writing In a similar vein, if a function is only reading, it should be defined with playground example that shows the "right" and "wrong" ways to write code.
I think it is an artifact from a time before I think it is better to keep people accidentally trying to modify state and things not working, or passing state to some APIs and expecting it to perform state management and it not working, etc. |
|
The issue here is the usage of the store - this forces the user to unbox every value that he reads from the store, and that's visible from the example, right? Although the value is only read and not mutated in any way, you are forced to unbox it from readonly for reading. TypeScipt has Readonly which does not force the user to unbox, but gives a compiler error on mutation. So, I strongly feel that the current typing is not good enough. |
|
If you can post playground examples, that'd be helpful. |
|
I guess it's better to use https://tsplay.dev/WkMEPN this doesn't shout at the dev sadly. 😞 |
|
https://tsplay.dev/WvVkBW this is actually great and will prevent shooting ourselves in the foot in case we try to do pure functions without immer. I vote on keeping it as it is. |
|
I feel this is more breaking than making the types. Can we instead provide an |
Pull Request Test Coverage Report for Build 1692494664Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
1 similar comment
Pull Request Test Coverage Report for Build 1692494664Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
After changing
Storeto be readonly, @high1 pointed out on discord that non-readonly types might be more desirable e.g. in this scenario:It's fairly common to encounter immutable functions that do not type their parameters as
readonlyoutside of native functions/classes likeObject.fromEntriesorMap. This PR makes it so none of these are readonly. However, I'm not sure ifsetStoreshould be kept as readonly.In my opinion:
setStore, there are fewer cases where a user might passlistinsetStoreto a function typed withstring[]if it is immutable, and more cases where users to try to directly modifylistwithinsetStoreif it is mutable.store, a user might attempt to call methods that do not exist on readonly arrays such asstore.list.push("uh oh")if it is mutable.Hence a discussion might be needed on whether it should be:
storeandsetStore: technically "correct" types, but might require castingstoreand readonlysetStore: less likely to get in most users' way, but allowsstoreto be misused e.g.store.list = [],store.list.push("uh oh"). Inconsistent.storeandsetStore: definitely will not get in users' way, but additionally allowssetStoreto be misused e.g.setStore(store => (store.list = [], store)).