Py学习  »  Redis

将Redis客户端导入Typescript应用程序时出错

MichaelG • 3 年前 • 1519 次点击  

当导入 npm Redis module 在TypeScript应用程序中,我收到错误:

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];

错误来自我的redis中的import语句。ts文件:

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;

下面是包中依赖项的子集。json文件:

{
    "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",
    ]
}

我尝试添加types包(@types/redis),但仍然显示错误。这似乎是Redis模块和TypeScript的问题。以前有人遇到过这个错误吗?提前感谢您的帮助。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133901
 
1519 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Steve
Reply   •   1 楼
Steve    3 年前

如果删除任何类型的代码,代码将在节点14上工作。我的包裹。json使用:

"@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();
Leibale Eidelman
Reply   •   2 楼
Leibale Eidelman    3 年前

@MichaelG node redis v4适用于TypeScript v4及以上版本,请尝试升级您的TypeScript引擎