Memoize functions - an optimization technique used to speed up consecutive function calls by caching the result of calls with identical input
by sindresorhusTypeScript
Last 12 weeks · 0 commits
4 of 6 standards met
Fixes #90 Warn when a function declares more than one parameter but memoization still uses the default first-argument cache key. The warning runs once when the wrapper is created, so repeated calls do not add hot-path noise. Supplying silences it. The test also verifies that single-parameter functions stay silent and that an explicit is treated as intentional. I removed one redundant type assertion flagged by the current XO toolchain so the canonical suite remains green after a fresh install. Tests: (XO, 68 AVA tests, TypeScript build, tsd) Implemented and tested with Codex assistance.
The return type of is unchanged, so the tag is only earned after the runtime guard. Fixes #114 Why not an property on the returned function? It changes the shape of the function, which defeats the point of mimicking the input exactly. It's also trivially forgeable and would show up in spreads, , and snapshots. The information already exists internally in a , so the check needs no extra surface. Why not brand the return type of by default? It would leak into every hover and error message, and can cause declaration-emit friction for consumers re-exporting a memoized function. A brand on the return value is also just an assertion, whereas the guard reflects reality: returns the input unchanged since nothing is cached, and correctly reports for it.
While this reads fine: This is bad practice: This could be enforced by a linter, since APIs like exist natively, but ideally and should accept a single parameter: It complicates types a bit but it requires the much cleaner: _Personal note:_ I went through this recently in webext-storage-cache, changing to
TypeScript users need to manually copy internal type definitions to create custom caches because and are not exported. Changes index.ts: Export and types test-d/index.test-d.ts: Add type tests verifying exported types work with custom cache implementations Usage Original prompt This section details on the original issue you should resolve* Defining custom cache in TypeScript Neither , nor are exported. So, in order to define a custom cache, I have to manually copy the types: or, a bit nicer: Could we have some exported types? ## Comments on the Issue (you are @copilot in this section) Fixes sindresorhus/memoize#111 💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
Repository: sindresorhus/memoize. Description: Memoize functions - an optimization technique used to speed up consecutive function calls by caching the result of calls with identical input Stars: 1163, Forks: 55. Primary language: TypeScript. Languages: TypeScript (100%). License: MIT. Latest release: v11.0.0 (5mo ago). Open PRs: 2, open issues: 2. Last activity: 1mo ago. Community health: 85%. Top contributors: sindresorhus, fregante, SamVerschueren, Richienb, BendingBender, bencehornak, coreyfarrell, dylang, elvinn, keithamus and others.