在快速会话中扩展会话对象 [英] Extending session object in express-session
问题描述
我知道这不是实际问题,而是需要帮助.
I know this is not an actual issue, but a help wanted.
我在打字稿和快速会话方面苦苦挣扎,而且我一直在玩耍,试图弄清楚这一点.
I'm struggling with typescript and express session and I have been playing around and trying to fiugure out this one for quite a bit.
我正在尝试扩展我的会话对象,为此,我正在尝试按照文档中的内容进行合并:
I'm trying to extend my session object, for that, I'm trying to do a merge of typings as per in the documentation:
我有一个 types/session.d.ts
与以下要合并的界面:
I have a types/session.d.ts
with the following interface to be merged:
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
,它确实可以工作:
However, if I import Session
from express-session
, it does work:
import { Session } from 'express-session'
declare module 'express-session' {
interface SessionData {
userId: string;
}
}
我对TypeScript不太熟练,我不确定是否可以在类型定义中导入模块,事件TypeScript对此表示警告(警告):
I'm not very proficient with TypeScript and I'm unsure about importing a module in a type defintion, event TypeScript complains about this (warning):
已声明会话",但从不读取其值.
'Session' is declared but its value is never read.
我想知道,这是解决问题的正确方法吗?
I'm wondering, is this the right way of tackling the problem?
我能做些什么?
亲切的问候!
PS:我的tsconfig应该很好,因为我可以通过代码中的其他类型定义使用它们,并且它们完全没有问题.
PS: my tsconfig should be fine as I have available through my code other type definitions and they are working with no issues at all.
推荐答案
You should use Module Augmentation. You should also know this from Modules:
在TypeScript中,就像在ECMAScript 2015中一样,任何包含顶级导入或导出的文件都被视为模块.相反,没有任何顶级导入或导出声明的文件将被视为脚本,其内容可在全局范围内使用(因此也可用于模块).
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
:请确保至少包含一个顶级 import
或 export
来完成此操作文件作为模块,而不是脚本的内容在全局范围内可用.有时,您将从第三方节点模块中导入和使用某些接口或类型.但是在您的情况下,您不需要它.因此,只需使用 export {}
或 import'express-session'
,两者都可以.
./types/session.d.ts
: Make sure you include at least one top-level import
or export
to make this file as a module, NOT a script whose contents are available in the global scope. Sometimes, you will import and use some interfaces or types from third-party node modules. But in your case, you don't need it. So just use export {}
or import 'express-session'
, both of them are ok.
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",
这篇关于在快速会话中扩展会话对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!