skip to Main Content

When importing the npm Redis module into a TypeScript application, I receive the error:

node_modules/@node-redis/client/dist/lib/client/index.d.ts:45:78 - error TS1256: A rest element must be last in a tuple type.
45 export declare type ClientLegacyCommandArguments = LegacyCommandArguments | [...LegacyCommandArguments, ClientLegacyCallback];

The error is from the import statement in my redis.ts file:

import { createClient } from 'redis';

class RedisCache {
    private client: any;

    async init() {

        const client = createClient();

        client.on('error', (err) => console.log('Redis Client Error', err));

        await client.connect();

        await client.set('key', 'value');
        const value = await client.get('key');
        console.log(value);
    }
}

const reditCache = new RedisCache();

export default reditCache;

Here is a subset of the dependencies from the package.json file:

{
    "dependencies": {
        "express": "4.17.1",
        "passport": "0.4.1",
        "passport-jwt": "^4.0.0",
        "redis": "^4.0.1",
        "typescript": "4.1.3"
    }
}

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "resolveJsonModule": true,
        "paths": {
            "*": [
                "node_modules/*",
                "src/types/*"
            ]
        }
    },
    "include": [
        "src/**/*",
        "src/locales/*.json",
    ]
}

I’ve tried adding the types package (@types/redis) but the error still shows. This seems to be an issue with the Redis module and TypeScript. Has anyone had experience with this error before? Thanks in advance for any help.

2

Answers


  1. @MichaelG node-redis v4 works with TypeScript v4 and up, try to upgrade your TypeScript engine

    Login or Signup to reply.
  2. Your code works on node14 if you remove the any type. My package.json uses:

    "@tsconfig/node14": "^1.0.1",
    "redis": "^4.0.4"
    
    class RedisCache {
        //private client: any;
    
        async init() {
    
            const client = createClient();
    
            client.on('error', (err) => console.log('Redis Client Error', err));
    
            await client.connect();
    
            await client.set('key', 'value');
            const value = await client.get('key');
            console.log("In class");
            console.log(value);
        }
    }
    
    const reditCache = new RedisCache();
    reditCache.init();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search