Skip to content

fix(store): fix inferrence of array types - #824

Closed
otonashixav wants to merge 1 commit into
solidjs:mainfrom
otonashixav:fix-produce-readonly-array
Closed

otonashixav wants to merge 1 commit into
solidjs:mainfrom
otonashixav:fix-produce-readonly-array

Conversation

@otonashixav

@otonashixav otonashixav commented Feb 1, 2022 •

Copy link
Copy Markdown
Contributor

Previously produce would wrongly infer all arrays from a store as readonly.
This fix makes the return type of produce StoreSetter instead, fixing the issue.
This change has a few caveats; namely, produce is less strictly typed: it should return a function that takes and returns DeepReadonly<T>, but now it returns all types contained in StoreSetter<T>. This has no consequence for normal usage other than better type inference.
Additionally to facilitate this change, DeepReadonly is no longer applied to the entire SetStoreFunction, but only where it is consumed (the setter and filter functions).

#790 would also fix this issue.

Previously produce would wrongly infer all arrays from a store as readonly.
This fix makes the return type of produce StoreSetter instead, fixing the issue.
This change has a few caveats; namely, produce is less strictly typed: it should return a function that takes and returns `DeepReadonly<T>`, but now it returns all types contained in `StoreSetter<T>`. This has no consequence for normal usage other than better type inference.
Additionally to facilitate this change, DeepReadonly is no longer applied to the entire SetStoreFunction, but only where it is consumed (the setter and filter functions).
@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 1777586321

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 89.454%

Totals Coverage Status
Change from base Build 1772005619: 0.0%
Covered Lines: 1197
Relevant Lines: 1278

💛 - Coveralls

| T
| Partial<T>
| ((prevState: T, traversed?: (keyof any)[]) => Partial<T> | void);
| ((prevState: DeepReadonly<T>, traversed?: (keyof any)[]) => Partial<DeepReadonly<T>> | void);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't gotten to test inference here yet. But if there's a way to fix the broken inference you mentioned, while keeping the "correct" readonly types, that'd be nice, so as to prevent people doing the wrong thing.

@otonashixav otonashixav Feb 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR changes two things and mostly keeps the readonly correctness:

  • The types of SetStoreFunction and StoreSetter such that DeepReadonly isn't applied directly at the root of the former, and instead only where the user would "interact" with the store in the latter.
  • produce now accepts all types that are part of the union of StoreSetter so that it can use the T type included in that type to properly type its own parameters, avoiding the inference issue. It minimally had to have its signature changed to include T without the rest of the union, but I think StoreSetter is more readable.

Do you mean that StoreSetter should also accept DeepReadonly variants of T | Partial<T>?

The alternative to this change would be a new DeepMutable type which marks all properties as mutable (-readonly), but that seems incorrect to me as the original type is available, and as it would also mark properties that were originally readonly (i.e. before being made into a store) as mutable.

@otonashixav
otonashixav marked this pull request as draft February 12, 2022 11:26
@otonashixav
otonashixav deleted the fix-produce-readonly-array branch March 15, 2022 18:44
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

Successfully merging this pull request may close these issues.

3 participants