-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve TypeScript type-safety of
cache.modify
(#10895)
* feat(cache): accept DELETE and INVALIDATE modifiers in the return type Problem: The `DELETE` and `INVALIDATE` modifiers type is `any`. Returning them from the `Modifier<T>` function triggers the `@typescript-eslint/no-unsafe-return` ESLint rule [0], since the modifier is then returning a value with type `any`. Solution: Set the types of these modifiers to two different `unique symbol`s. These serve as unique opaque types. The consuming code should not peek into them. These 2 types are permitted in the return type of the `Modifier<T>` function, since they are not `any` anymore, and thus, are not assignable to `T`. This change only affects TypeScript types. It has no runtime impact. [0]: https://typescript-eslint.io/rules/no-unsafe-return/ * feat(cache): type inference for cache `Modifiers` type Problem: The `fields` property in `cache.modify` did not offer ways to infer the field names or values based on the field type. It accepted any field names and the field values were `any`. Solution: Add a generic type parameter for the object type to the `Modifiers` type. The code can now use the `satisfies Modifiers<...>` operator to inform TypeScript about the possible field names and values. Field values include `Reference`s for objects and arrays. The consuming code is then responsible for checking (or asserting) that the field value is or is not a reference. * refactor(cache): add default value for Modifiers generic parameter Makes the change backwards-compatible. * refactor(cache): use interfaces for invalidate and delete modifier types Interfaces retain their names in error messages and hover dialogs. Type aliases did not. * refactor(cache): add generic parameter for ModifyOptions A generic parameter is easier to discover and set than `fields: { ... } satisfies Modifiers<...>` * refactor(cache): use Record<string, any> as the default Entity type Use a more appropriate real-life default type for `cache.modify`. * code review adjustments * refactor(cache-test): replace assertType with expect-type library Use an already installed library instead of introducing a new utility function. * chore: use generic parameter instead of satisfies in changeset Promote the use of a generic parameter of `cache.modify` over using `satisfies` which requires importing the `Modifers` type. * fix(cache): use AllFieldsModifier type in cache.modify parameter --------- Co-authored-by: Lenz Weber-Tronic <[email protected]>
- Loading branch information
Showing
10 changed files
with
295 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
"@apollo/client": minor | ||
--- | ||
|
||
Add generic type parameter for the entity modified in `cache.modify`. Improves | ||
TypeScript type inference for that type's fields and values of those fields. | ||
|
||
Example: | ||
|
||
```ts | ||
cache.modify<Book>({ | ||
id: cache.identify(someBook), | ||
fields: { | ||
title: (title) => { | ||
// title has type `string`. | ||
// It used to be `any`. | ||
}, | ||
author: (author) => { | ||
// author has type `Reference | Book["author"]`. | ||
// It used to be `any`. | ||
}, | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@apollo/client": minor | ||
--- | ||
|
||
Use unique opaque types for the `DELETE` and `INVALIDATE` Apollo cache modifiers. | ||
|
||
This increases type safety, since these 2 modifiers no longer have the `any` type. | ||
Moreover, it no longer triggers [the `@typescript-eslint/no-unsafe-return` | ||
rule](https://typescript-eslint.io/rules/no-unsafe-return/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.