如何在打字稿中设置 "connect-redis"

how to set "connect-redis" in typescript

现在我正在学习 Fullstack React GraphQL TypeScript 教程

我在使用 express-session 连接 Redis 时遇到问题;;;

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

尝试回滚到

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

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


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

我有一个类似的问题,但令人惊讶的是(老实说,不完全确定为什么会这样,但无论如何..)保留“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",

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