-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve TypeScript type-safety of cache.modify
#10895
Improve TypeScript type-safety of cache.modify
#10895
Commits on Jun 12, 2023
-
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/
Configuration menu - View commit details
-
Copy full SHA for 0faf222 - Browse repository at this point
Copy the full SHA 0faf222View commit details -
feat(cache): type inference for cache
Modifiers
typeProblem: 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.
Configuration menu - View commit details
-
Copy full SHA for 5f84a0b - Browse repository at this point
Copy the full SHA 5f84a0bView commit details -
refactor(cache): add default value for Modifiers generic parameter
Makes the change backwards-compatible.
Configuration menu - View commit details
-
Copy full SHA for 4568407 - Browse repository at this point
Copy the full SHA 4568407View commit details -
refactor(cache): use interfaces for invalidate and delete modifier types
Interfaces retain their names in error messages and hover dialogs. Type aliases did not.
Configuration menu - View commit details
-
Copy full SHA for 9f7d536 - Browse repository at this point
Copy the full SHA 9f7d536View commit details -
refactor(cache): add generic parameter for ModifyOptions
A generic parameter is easier to discover and set than `fields: { ... } satisfies Modifiers<...>`
Configuration menu - View commit details
-
Copy full SHA for 1cfb03b - Browse repository at this point
Copy the full SHA 1cfb03bView commit details -
refactor(cache): use Record<string, any> as the default Entity type
Use a more appropriate real-life default type for `cache.modify`.
Configuration menu - View commit details
-
Copy full SHA for 014f17d - Browse repository at this point
Copy the full SHA 014f17dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 6973cd4 - Browse repository at this point
Copy the full SHA 6973cd4View commit details -
refactor(cache-test): replace assertType with expect-type library
Use an already installed library instead of introducing a new utility function.
Configuration menu - View commit details
-
Copy full SHA for 4c12626 - Browse repository at this point
Copy the full SHA 4c12626View commit details -
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.
Configuration menu - View commit details
-
Copy full SHA for 18e8bf9 - Browse repository at this point
Copy the full SHA 18e8bf9View commit details -
Configuration menu - View commit details
-
Copy full SHA for fc50711 - Browse repository at this point
Copy the full SHA fc50711View commit details