3

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.

1
  • 1
    It's possible the latest version of the Redis module isn't ready for TypeScript, it's only a few months old at this time. Downgrading to an older version is working. Commented Jan 10, 2022 at 16:46

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

good answer, I like it...
0

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

1 Comment

Thanks for the response Leibale, I'm running TypeScript version 4.1.3. I've updated the question to include this information in the package.json file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.