Skip to content

Conversation

Copy link

Copilot AI commented Nov 11, 2025

TypeScript users need to manually copy internal type definitions to create custom caches because CacheItem and CacheLike are not exported.

Changes

  • index.ts: Export CacheItem<ValueType> and CacheLike<KeyType, ValueType> types
  • test-d/index.test-d.ts: Add type tests verifying exported types work with custom cache implementations

Usage

import memoize, { type CacheItem, type CacheLike } from 'memoize';

// Type cache values directly
const cache = new Map<string, CacheItem<number>>();

// Or implement custom cache with proper typing
const customCache: CacheLike<string, number> = {
  has: (key) => cache.has(key),
  get: (key) => cache.get(key),
  set: (key, value) => cache.set(key, value),
  delete: (key) => cache.delete(key),
  clear: () => cache.clear(),
};

const memoized = memoize(expensiveFunction, { cache: customCache });
Original prompt

This section details on the original issue you should resolve

<issue_title>Defining custom cache in TypeScript</issue_title>
<issue_description>Neither CacheItem, nor CacheLike are exported.
So, in order to define a custom cache, I have to manually copy the types:

// https://github.com/sindresorhus/memoize/blob/4c2b35fad5841cea20274b461fe2528fa40a692d/index.ts#L12-L15
type CacheItem<ValueType> = {
	data: ValueType;
	maxAge: number;
};

const cacheMap = new Map<Key, CacheItem<Value>>();

or, a bit nicer:

// https://github.com/sindresorhus/memoize/blob/4c2b35fad5841cea20274b461fe2528fa40a692d/index.ts#L12C1-L23C3
type CacheItem<ValueType> = {
	data: ValueType;
	maxAge: number;
};

type CacheLike<KeyType, ValueType> = {
	has: (key: KeyType) => boolean;
	get: (key: KeyType) => CacheItem<ValueType> | undefined;
	set: (key: KeyType, value: CacheItem<ValueType>) => void;
	delete: (key: KeyType) => void;
	clear?: () => void;
};

const cacheMap: CacheLike<Key, Value> = new Map();

Could we have some exported types?</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 11, 2025
Copilot AI changed the title [WIP] Define custom cache types in TypeScript Export CacheItem and CacheLike types for TypeScript custom cache definitions Nov 11, 2025
Copilot AI requested a review from fregante November 11, 2025 08:48
Copilot finished work on behalf of fregante November 11, 2025 08:48
Copy link
Collaborator

@fregante fregante left a comment

Choose a reason for hiding this comment

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

@Richienb this?

@fregante fregante marked this pull request as ready for review November 11, 2025 09:03
@Richienb
Copy link
Contributor

Richienb commented Nov 11, 2025

@fregante Yes, exporting the types is enough.

So it depends whether would be a good design decision to only export one of them (CacheLike), rather than both.

// Test using CacheLike in memoize options
const customCacheMap: CacheLike<string, number> = new Map();
const memoizedWithCustomCache = memoize(Number, {cache: customCacheMap});
expectType<typeof Number>(memoizedWithCustomCache);
Copy link
Owner

Choose a reason for hiding this comment

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

These changes are not needed. We don't need tests to confirm that, yes, the two added export keywords do indeed work.

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.

Defining custom cache in TypeScript

4 participants