skip to Main Content

Now im following the Fullstack React GraphQL TypeScript Tutorial

I get trouble in connectRedis with express-session;;;

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

...

const RedisStore = connectRedis(session);
[ERROR]
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);

2

Answers


  1. Try rolling back to

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

    I have similar problems, and rolling back helps, although I do not think it is a good practice. I believe something wrong about typing in the new versions of these package


    Well, I tried using the newest packages again, and remove "dist" and "tsc -w" again. There is no such error. So not sure what exactly the mechanism is.

    Login or Signup to reply.
  2. I had a similar problem but surprisingly enough (and honestly, not entirely sure why this is so but anyway..) keeping the "require" instead of converting to "import" solved this problem for me.
    When I had redis setup exactly as the documentation from connect-redis, it seems to work fine but somehow converting to import created problems.
    If anyone can explain why this is happening, would be much appreciated!

    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,
      })
    )
    

    FYI here are my package versions (all latest versions up to date):

    "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",
    

    I ran into this problem while following Ben Awad’s Fullstack React GraphQL Typescript Tutorial on youtube and when i undid the import conversion, code seemed to work well lol

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search