Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when importing Redis client into Typescript app

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.

like image 451
MichaelG Avatar asked Nov 17 '25 13:11

MichaelG


1 Answers

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();
like image 124
steve Avatar answered Nov 19 '25 09:11

steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!