refactor: update logic for all metadata query factories #10356
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Connected to #10312
Changes made
getMetadataAsJSON
functionProblems and future pitfalls
Unfortunately, while the runtime code is cleaner and no longer requires the query client, there's still a problem, which is that it's hard to make React Query and TypeScript happy.
Every single factory function must have an explicit return type, or else there is a risk of the
undefined
case fromgetMetadataAsJSON
will pollute the query cache's types. It has to be an explicit return type (and not something likesatisfies
or no type annotations at all), because the return type needs to do some implicit type casting.Let's say we have a component like this:
This is the type that
data
will be with each approach for typing theappearance
function:AppearanceConfig | undefined
- The cache's types got pollutedsatisfies
- Has typeAppearanceConfig | undefined
- The cache's types still got polluted, because the underlying type didn't changeas const satisfies
- Has typeAppearanceConfig | undefined
- Even with the object being treated as more of a type literal, the underlying type still doesn't change enoughAppearanceConfig
- No pollution. This is the only option that works, because the explicit return type is overriding the type that the object would normally beIt's a weird position to be in, where there's only one right way to set up the factory, or else we basically break React Query's internal type system
Next steps
This PR is meant to be more of a stopgap until we figure out a way to avoid these type issues in a more permanent way