简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM