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

refactor: improve type definitions for CacheOptions #2700

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from

Conversation

Rewwoken
Copy link

@Rewwoken Rewwoken commented Sep 1, 2024

πŸ”— Linked issue

N/A

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Hi!

This PR adds argument type inheritance from defineCachedFunction and defineCachedEventHandler to the arguments of functions in the CacheOptions interface.

I think this will improve DX, because:

  1. No need to explicitly type arguments for every CacheOptions object.
  2. TypeScript will show an error in case of type incompatability

Example

We have some function we want to cache:

// some service.ts
import type { FooObj } from '...';

export function foo(a: number, obj: FooObj) {
  // ...
}

Before:

// some cache.ts
import { foo } from '...';
import type { FooObj } from '...';

export const cachedFoo = cachedFunction(
  foo,
  {
    // Arguments are type of `any`, so need to import `FooObj` and hardcode first argument as `number`
    getKey: (a: number, obj: FooObj) => generateFooCacheKey(obj),
    maxAge: 1000,
  },
);

After:

// some cache.ts
import { foo } from '...';

export const cachedFoo = cachedFunction(
  foo,
  {
    // Arguments type is inherited from foo, no need to import `FooObj` or hardcode
    getKey: (a, obj) => generateFooCacheKey(obj),
    maxAge: 1000,
  },
);

I don't think docs are needed to updated, since explicitly typing is still a valid case.

TypeScript WILL complain if arguments are improperly typed explicitly. I’m curious if this counts as a breaking change.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant