'Session | undefined' 类型的参数不能分配给 'Session' 类型的参数
Argument of type 'Session | undefined' is not assignable to parameter of type 'Session'
这是我的代码的简化版本:
npm install express express-cookies @types/cookie-session @types/express-session
import express from express();
import cookieSession from 'cookie-session';
const app = express();
app.use(cookieSession({
name: 'session',
keys: [
process.env.SESSION_KEY as string
]
}));
const router = express.Router();
const changeSession = (session = express.Session) => {
session.foo = 'bar'
}
router.get((req, res) => {
changeSession(req.session)
});
在 changeSession(req.session)
我收到错误:
Argument of type 'Session | undefined' is not assignable to parameter of type 'Session'.
Type 'undefined' is not assignable to type 'Session'
当我使用 app.get
而不是 router.get
时也会发生同样的情况
不确定为什么 express-cookies 没有正确地将会话对象注册到请求。
这里有一个 link 到 @types/cookie-session: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cookie-session/index.d.ts 为 express-cookies
提供类型
有什么帮助吗?
错误非常明显,express.Session
可能是 undefined
并且 changeSession
函数被声明为期望 Session
类型的参数(不是 Session | undefined
).
如果您确定您的 express.Session
对象不会 undefined
,您可以像这样分配默认参数值
const changeSession = (session = express.Session!) => {
session.foo = 'bar'
}
注意值后面的感叹号 (!)。它迫使编译器忘记 undefined
值。
这非常棘手,当然,如果此 express.Session
为 undefined
.
,您可能会遇到运行时异常
希望对您有所帮助。
express-cookies 类型指定 req.session
可以是 undefined
。据我了解,仅当您使用 express 注册 cookieSession
中间件时,才会定义 req.session
。因此,如果出于任何原因您不注册此中间件(例如,删除错误注册它的代码),req.session
将是未定义的。
因为会话中间件有可能没有被注册,所以在类型上期望 req.session
可能是 undefined
.
是正确的
因此,使用 TS 您需要在使用它之前检查 req.session
是否已定义:
if (req.session) {
changeSession(req.session)
}
或者如果会话对于路由是必需的,则显式抛出错误:
if (!req.session) {
throw new Error('Session is missing.');
}
changeSession(req.session)
或者不得已,用感叹号告诉TS实际上定义了req.session
:
changeSession(req.session!)
但这不是类型安全的。
这是我的代码的简化版本:
npm install express express-cookies @types/cookie-session @types/express-session
import express from express();
import cookieSession from 'cookie-session';
const app = express();
app.use(cookieSession({
name: 'session',
keys: [
process.env.SESSION_KEY as string
]
}));
const router = express.Router();
const changeSession = (session = express.Session) => {
session.foo = 'bar'
}
router.get((req, res) => {
changeSession(req.session)
});
在 changeSession(req.session)
我收到错误:
Argument of type 'Session | undefined' is not assignable to parameter of type 'Session'.
Type 'undefined' is not assignable to type 'Session'
当我使用 app.get
而不是 router.get
不确定为什么 express-cookies 没有正确地将会话对象注册到请求。
这里有一个 link 到 @types/cookie-session: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cookie-session/index.d.ts 为 express-cookies
有什么帮助吗?
错误非常明显,express.Session
可能是 undefined
并且 changeSession
函数被声明为期望 Session
类型的参数(不是 Session | undefined
).
如果您确定您的 express.Session
对象不会 undefined
,您可以像这样分配默认参数值
const changeSession = (session = express.Session!) => {
session.foo = 'bar'
}
注意值后面的感叹号 (!)。它迫使编译器忘记 undefined
值。
这非常棘手,当然,如果此 express.Session
为 undefined
.
希望对您有所帮助。
express-cookies 类型指定 req.session
可以是 undefined
。据我了解,仅当您使用 express 注册 cookieSession
中间件时,才会定义 req.session
。因此,如果出于任何原因您不注册此中间件(例如,删除错误注册它的代码),req.session
将是未定义的。
因为会话中间件有可能没有被注册,所以在类型上期望 req.session
可能是 undefined
.
因此,使用 TS 您需要在使用它之前检查 req.session
是否已定义:
if (req.session) {
changeSession(req.session)
}
或者如果会话对于路由是必需的,则显式抛出错误:
if (!req.session) {
throw new Error('Session is missing.');
}
changeSession(req.session)
或者不得已,用感叹号告诉TS实际上定义了req.session
:
changeSession(req.session!)
但这不是类型安全的。