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

Merged
merged 4 commits into from
Sep 29, 2024
Merged

Conversation

Rewwoken
Copy link
Contributor

@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.

@pi0 pi0 added the cache label Sep 20, 2024
@pi0 pi0 self-requested a review as a code owner September 29, 2024 22:23
Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice thanks โค๏ธ

@pi0 pi0 merged commit 43d4b20 into nitrojs:v2 Sep 29, 2024
4 checks passed
@pi0 pi0 mentioned this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants