Skip to content
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

Merged
merged 10 commits into from
Jun 13, 2023

Commits on Jun 12, 2023

  1. 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/
    Gelio authored and phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    0faf222 View commit details
    Browse the repository at this point in the history
  2. 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.
    Gelio authored and phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    5f84a0b View commit details
    Browse the repository at this point in the history
  3. refactor(cache): add default value for Modifiers generic parameter

    Makes the change backwards-compatible.
    Gelio authored and phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    4568407 View commit details
    Browse the repository at this point in the history
  4. refactor(cache): use interfaces for invalidate and delete modifier types

    Interfaces retain their names in error messages and hover dialogs. Type
    aliases did not.
    Gelio authored and phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    9f7d536 View commit details
    Browse the repository at this point in the history
  5. refactor(cache): add generic parameter for ModifyOptions

    A generic parameter is easier to discover and set than
    `fields: { ... } satisfies Modifiers<...>`
    Gelio authored and phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    1cfb03b View commit details
    Browse the repository at this point in the history
  6. refactor(cache): use Record<string, any> as the default Entity type

    Use a more appropriate real-life default type for `cache.modify`.
    Gelio authored and phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    014f17d View commit details
    Browse the repository at this point in the history
  7. code review adjustments

    phryneas committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    6973cd4 View commit details
    Browse the repository at this point in the history
  8. refactor(cache-test): replace assertType with expect-type library

    Use an already installed library instead of introducing a new utility
    function.
    Gelio committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    4c12626 View commit details
    Browse the repository at this point in the history
  9. 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.
    Gelio committed Jun 12, 2023
    Configuration menu
    Copy the full SHA
    18e8bf9 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    fc50711 View commit details
    Browse the repository at this point in the history