Skip to content

Defining custom cache in TypeScript #111

@Richienb

Description

@Richienb

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?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions