【问题标题】:how to set "connect-redis" in typescript如何在打字稿中设置“connect-redis”
【发布时间】:2021-05-04 22:08:10
【问题描述】:

现在我正在关注 Fullstack React GraphQL TypeScript 教程

我在使用 express-session 的 connectRedis 中遇到问题;;;

import connectRedis from "connect-redis";
import session from "express-session";

...

const RedisStore = connectRedis(session);

[错误]

error TS2345: Argument of type 'typeof session' is not assignable to parameter of type '(options?: SessionOptions | undefined) => RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Types of parameters 'options' and 'options' are incompatible.
Type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/connect-redis/node_modules/@types/express-session/index").SessionOptions | undefined' is not assignable to type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/express-session/index").SessionOptions | undefined'.
  Type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/connect-redis/node_modules/@types/express-session/index").SessionOptions' is not assignable to type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/express-session/index").SessionOptions'.
    Types of property 'store' are incompatible.
      Type 'Store | undefined' is not assignable to type 'Store | MemoryStore | undefined'.
        Type 'Store' is not assignable to type 'Store | MemoryStore | undefined'.
          Type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/connect-redis/node_modules/@types/express-session/index").Store' is not assignable to type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/express-session/index").Store'.
            Types of property 'load' are incompatible.
              Type '(sid: string, callback: (err: any, session?: SessionData | undefined) => any) => void' is not assignable to type '(sid: string, fn: (err: any, session?: SessionData | null | undefined) => any) => void'.
                Types of parameters 'callback' and 'fn' are incompatible.
                  Types of parameters 'session' and 'session' are incompatible.
                    Type 'SessionData | undefined' is not assignable to type 'SessionData | null | undefined'.
                      Type 'import("/Users/jsyovo/Documents/2-MyRepository/2-ReactJS/14-reddit-clone/server/node_modules/@types/connect-redis/node_modules/@types/express-session/index").SessionData' is not assignable to type 'Express.SessionData'.
                        The types of 'cookie.path' are incompatible between these types.
                          Type 'string | undefined' is not assignable to type 'string'.
                            Type 'undefined' is not assignable to type 'string'.

22     const RedisStore = connectRedis(session);

【问题讨论】:

    标签: typescript express redis express-session


    【解决方案1】:

    我遇到了类似的问题,但令人惊讶的是(老实说,不完全确定为什么会这样,但无论如何......)保留“require”而不是转换为“import”为我解决了这个问题。 当我完全按照 connect-redis 中的文档设置 redis 时,它似乎工作正常,但不知何故转换为导入会产生问题。 如果有人能解释为什么会这样,将不胜感激!

    const redis = require('redis')
    const session = require('express-session')
    
    let RedisStore = require('connect-redis')(session)
    let redisClient = redis.createClient()
    
    app.use(
      session({
        store: new RedisStore({ client: redisClient }),
        saveUninitialized: false,
        secret: 'keyboard cat',
        resave: false,
      })
    )
    

    仅供参考,这是我的软件包版本(所有最新版本都是最新的):

    "connect-redis": "^6.0.0",
    "express-session": "^1.17.2",
    "redis": "^4.0.0",
    "@types/connect-redis": "^0.0.17",
    "@types/express-session": "^1.17.4",
    "@types/redis": "^2.8.32",
    

    我在 youtube 上关注 Ben Awad 的 Fullstack React GraphQL Typescript 教程时遇到了这个问题,当我取消导入转换时,代码似乎运行良好,哈哈

    【讨论】:

      【解决方案2】:

      尝试回滚到

      "@types/express-session": "1.17.1"
      "@types/connect-redis": "^0.0.14"
      

      我有类似的问题,回滚会有所帮助,尽管我认为这不是一个好习惯。我认为输入这些包的新版本有问题


      好吧,我再次尝试使用最新的软件包,并再次删除“dist”和“tsc -w”。没有这样的错误。所以不确定机制到底是什么。

      【讨论】:

      • Tkanks 兄弟!!!我换成旧版本就可以了!!
      • 我相信你只需清理缓存或 dist 之类的东西,它应该可以工作。
      • 删除 yarn.lock 并重新安装 deps 有帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2017-09-15
      • 2020-05-29
      • 2018-12-30
      • 2018-04-02
      • 1970-01-01
      相关资源
      最近更新 更多