【发布时间】:2021-04-18 02:34:49
【问题描述】:
我对 Typescript 比较陌生,我正在尝试在 express-session 的 req.session 对象中声明一个属性。
我试过这个,但它一直给我错误“req”与“req”不兼容?!?
完整的错误:
No overload matches this call.
Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
Argument of type '(req: Request<ParamsDictionary, any, any, ParsedQs>, res: Response<any>) => void' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Types of parameters 'req' and 'req' are incompatible.
Type 'import("/home/arvore/Documents/arvore/api/node_modules/@types/express-serve-static-core/index").Request<import("/home/arvore/Documents/arvore/api/node_modules/@types/express-serve-static-core/index").ParamsDictionary, any, any, qs.ParsedQs>' is not assignable to type 'import("/home/arvore/Documents/arvore/api/node_modules/@types/express/index").Request<import("/home/arvore/Documents/arvore/api/node_modules/@types/express-serve-static-core/index").ParamsDictionary, any, any, qs.ParsedQs>'.
Types of property 'session' are incompatible.
Property 'user' is missing in type 'Session & Partial<SessionData>' but required in type 'sessionData'.
Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
Argument of type '(req: Request<ParamsDictionary, any, any, ParsedQs>, res: Response<any>) => void' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>'.
Type '(req: Request<ParamsDictionary, any, any, ParsedQs>, res: Response<any>) => void' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Overload 3 of 3, '(path: PathParams, subApplication: Application): Router', gave the following error.
Argument of type '(req: Request<ParamsDictionary, any, any, ParsedQs>, res: Response<any>) => void' is not assignable to parameter of type 'Application'.
Type '(req: Request<ParamsDictionary, any, any, ParsedQs>, res: Response<any>) => void' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 61 more.
import { Session, SessionData } from "express-session";
interface userInformation {
username: string,
email: string,
}
interface sessionData extends Session {
user: userInformation
}
declare module "express" {
interface Request {
session: sessionData
}
}
编辑:
我正在使用的控制器:
import { Request, Response } from "express";
class AppAPIController {
public getUserByAuth(req: Request, res: Response) {
return res.json(req.session.user)
}
}
export default new AppAPIController;
我的路线文件:
import { Router } from "express";
import AppAPIController from "./controllers/AppAPIController";
const routes = Router();
routes.get("/@me", AppAPIController.getUserByAuth);
【问题讨论】:
-
分享完整的错误
-
@Evert 我刚刚编辑了完整的错误。
-
这能回答你的问题吗:stackoverflow.com/questions/38900537/…
-
@Evert 不,现在“req.session”中甚至不存在“user”属性
标签: node.js typescript express express-session