I'm trying to use express-session and express-mysql-session within a Typescript project. Here's the relevant part of my code:
import * as express from "express";
import * as expressSession from "express-session";
import * as expressMySqlSession from "express-mysql-session";
this.express = express();
const sessionStore = new expressMySqlSession(sessionStoreConfig, this.dbConnection);
const sessionHandler = expressSession({
...,
store: sessionStore
});
this.express.use(sessionHandler);
It does not compile because the store
option is of type expressSession.Store | expressSession.MemoryStore | undefined
while sessionStore
is of type MySQLStore
.
What am I doing wrong?
Regards
Try this:
import * as express from "express";
import * as expressSession from "express-session";
import expressMySqlSession from "express-mysql-session";
const MySQLStore = expressMySqlSession(expressSession);
const sessionStore = new MySQLStore(sessionStoreConfig, this.dbConnection);
...
With both express-session
, express-mysql-session
, and @types/express-mysql-session
listed as dependencies:
Current version of the types (from about two years ago) tracks an older version of express-mysql-session
, where the default export is the class. Fortunately, there's some backwards-compatibility in the library for this older version, so you can instantiate it directly:
import MySQLSessionStore from "express-mysql-session";
const store = new MySQLSessionStore({...});
And the library will handle importing the express-session
dependency for you.
Code never lies!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With