在快速会话中扩展会话对象

Extending session object in express-session

我知道这不是实际问题,但需要帮助。

我正在为 typescript 和 express session 而苦苦挣扎,我一直在玩弄并试图弄清楚这个问题。

我正在尝试扩展我的会话对象,为此,我正在尝试按照文档中的说明进行类型合并:

我有一个 types/session.d.ts 需要合并以下界面:

declare module 'express-session' {
  interface SessionData {
    userId: string;
  }
}

但这不起作用,例如,在 other-folder/some.ts

req.session.userId = user.id;
// Property 'userId' does not exist on type 'Session & Partial<SessionData>'.

但是,如果我从 express-session 导入 Session,它确实有效:

import { Session } from 'express-session'

declare module 'express-session' {
  interface SessionData {
    userId: string;
  }
}

我不是很精通 TypeScript,我不确定在类型定义中导入模块,事件 TypeScript 抱怨这个(警告):

'Session' 已声明,但其值从未被读取。

我想知道,这是解决问题的正确方法吗?

我能做什么不同的事情?

亲切的问候!

PS:我的 tsconfig 应该没问题,因为我可以通过我的代码使用其他类型定义,并且它们完全没有问题。

你应该使用 Module Augmentation. You should also know this from Modules:

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

例如:

./src/main.ts:

import express from 'express';
import session from 'express-session';

const app = express();

app.use(
  session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true },
  }),
);
app.get('/', (req, res) => {
  const user = { id: '1' };
  req.session.userId = user.id;
});

./types/session.d.ts:确保至少包含一个顶级 importexport 以使此文件成为一个模块,而不是其内容在全局可用的脚本范围。有时,您会导入并使用来自第三方节点模块的一些接口或类型。但就您而言,您不需要它。所以用export {}或者import 'express-session'都可以。

declare module 'express-session' {
  interface SessionData {
    userId: string;
  }
}

export {};

tsconfig.json:

"typeRoots": [
  "./node_modules/@types",
  "./types",
], 

包版本:

"express": "^4.17.1",
"@types/express": "^4.17.11",
"typescript": "^3.9.7"
"express-session": "^1.17.1",
"@types/express-session": "^1.17.3",