-
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.
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.
- Loading branch information
Showing
6 changed files
with
126 additions
and
36 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,27 @@ | ||
--- | ||
"@apollo/client": minor | ||
--- | ||
|
||
Add generic type parameter for the cache `Modifiers` type. Improves TypeScript | ||
type inference for that type's fields and values of those fields. | ||
|
||
Example: | ||
|
||
```ts | ||
cache.modify({ | ||
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`. | ||
}, | ||
} satisfies Modifiers<Book>, | ||
}); | ||
``` | ||
|
||
To take advantage of the type inference, use [the `satisfies Modifiers<...>` | ||
operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html). |
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
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