You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Named Return Type for createCache() Method in cache-manager
Description
The createCache() method in the cache-manager package currently lacks a named return type. This results in challenges for developers working with TypeScript, particularly in dependency injection frameworks like NestJS. Without a named return type, users must define their own types or rely on any, which undermines type safety and maintainability.
Problem Statement
In cache-manager v6, the createCache() method returns an object with various methods such as get, set, del, etc. However, this return type is inferred and not explicitly defined as a named type. This creates the following issues:
The return type of the cache instance must be manually defined in the provider or service.
Users cannot leverage TypeScript's type inference and autocomplete effectively.
Updates to the cache-manager package could inadvertently break user implementations due to lack of strong type definitions.
exportclassExampleService{constructor(
@Inject('CACHE_MANAGER')privatereadonlycache: any,// there is no named type in the package){}asyncgetCachedData(key: string): Promise<any>{returnthis.cache.get(key);// IDE has no idea about the syntax here, since this.cache is any}}
The cache property is typed as any, which undermines the benefits of using TypeScript.
Suggested Solution
Introduce a new named interface, CacheInstance, that explicitly defines the shape of the object returned by createCache(). The method would then return this named type, improving type safety and usability.
This change would greatly enhance the developer experience and ensure stronger type guarantees for users of the cache-manager package, particularly in TypeScript projects. A named return type would also help maintain compatibility during future updates to the package.
The text was updated successfully, but these errors were encountered:
Issue Title
Add Named Return Type for
createCache()
Method incache-manager
Description
The
createCache()
method in thecache-manager
package currently lacks a named return type. This results in challenges for developers working with TypeScript, particularly in dependency injection frameworks like NestJS. Without a named return type, users must define their own types or rely onany
, which undermines type safety and maintainability.Problem Statement
In
cache-manager
v6, thecreateCache()
method returns an object with various methods such asget
,set
,del
, etc. However, this return type is inferred and not explicitly defined as a named type. This creates the following issues:cache-manager
package could inadvertently break user implementations due to lack of strong type definitions.Example of the current usage in a NestJS service:
The
cache
property is typed asany
, which undermines the benefits of using TypeScript.Suggested Solution
Introduce a new named interface,
CacheInstance
, that explicitly defines the shape of the object returned bycreateCache()
. The method would then return this named type, improving type safety and usability.Proposed Interface
Updated Method Signature
Benefits
CacheInstance
directly in their code, ensuring compatibility with the latestcache-manager
changes.cache-manager
instance without defining custom types.Example Usage in NestJS
Additional Notes
This change would greatly enhance the developer experience and ensure stronger type guarantees for users of the
cache-manager
package, particularly in TypeScript projects. A named return type would also help maintain compatibility during future updates to the package.The text was updated successfully, but these errors were encountered: