node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

Type instantiation is excessively deep and possibly infinite in RedisJSON.GET

Open mauriciocirelli opened this issue 1 year ago • 3 comments

Description

Dear,

I am facing a Typescript issue using redisClient.json.get(key) function:

Type instantiation is excessively deep and possibly infinite

Has anyone seen this before?

Node.js Version

v20.11.0

Redis Server Version

7.2.4 (redis-stack)

Node Redis Version

4.6.15

Platform

Docker (Linux Container)

Logs

No response

mauriciocirelli avatar Sep 09 '24 20:09 mauriciocirelli

Hi,

Could fix it by casting it: (await redisClient.json.get(key)) as any

mauriciocirelli avatar Sep 10 '24 11:09 mauriciocirelli

what even the point of writing it in ts when you have to typecast every single thing ?

ninox14 avatar Feb 19 '25 18:02 ninox14

Resolving TypeScript Error TS2589 on client.json.get()

This issue (Type instantiation is excessively deep and possibly infinite) arises because the default return type of client.json.get() is a deeply complex generic, hitting TypeScript's internal recursion limit. Here are the effective solutions, ordered by preference.

1. Primary Fix: Explicitly Provide the Generic Type (Recommended)

This is the most robust, type-safe, and simplest fix. It immediately solves the recursion problem by providing the type the compiler is struggling to infer.

Action: Always pass your expected data type as a generic parameter to the method.

// 1. Define the specific structure you expect
interface OrderDetails {
    orderId: string;
    total: number;
    items: Array<{ productId: string; quantity: number }>;
}

const key = 'user:order:456';

//  Fix: Pass the type explicitly to halt deep inference.
const order = await redisClient.json.get<OrderDetails>(key, '$'); 

if (order) {
    console.log(order.total); // Fully type-safe access
}

2. Library-Side Resolution: Type Narrowing for Generic Default (For Maintainers)

For the library maintainers, one potential structural fix would be to simplify the default generic type definition (T in json.get<T>).

The issue: The default type for T is likely a recursive type intended to cover any possible JSON structure.

The Solution Concept (Using Utility Types): Maintainers could potentially leverage a utility type or conditional logic to internally cap the depth of the default return type, thereby preventing the recursion limit from being hit when the generic is omitted. This would be complex but would improve the developer experience when fetching simple objects.

3. Last Resort: Increasing TypeScript's Recursion Limit (Not Recommended)

If you are dealing with very deep or recursive application models that cause the error even when using generics, you can increase the compiler's internal limit. This should be avoided unless absolutely necessary as it can slow down compilation.

Action: Add or modify the typeScriptNestingLevel in your tsconfig.json.

{
  "compilerOptions": {
    "module": "commonjs",
    // ... other options
    "typeScriptNestingLevel": 100 // Default is ~50. Increase with caution.
  }
}

watersRand avatar Nov 01 '25 08:11 watersRand