无法在TypeScript中扩展Express Request [英] Unable to extend Express Request in TypeScript
问题描述
这个问题已经问了好几次了,但是给出的答案对我来说都没有用.我正在尝试扩展Express Request
对象以包括存储User
对象的属性.我创建了一个声明文件express.d.ts
,并将其与我的tsconfig.json
放在同一目录中:
This question has been asked several times, but none of the answers that were given have worked for me. I'm trying to extend the Express Request
object to include a property to store a User
object. I created a declaration file, express.d.ts
, and placed it in the same directory as my tsconfig.json
:
import { User } from "./src/models/user";
declare namespace Express {
export interface Request {
user: User;
}
}
然后我尝试在secured-api.ts
中对其进行分配:
Then I try to make an assignment to it in secured-api.ts
:
import express from 'express';
import { userService } from '../services/user';
router.use(async (req, res, next) => {
try {
const user = await userService.findByUsername(payload.username);
// do stuff to user...
req.user = user;
next();
} catch(err) {
// handle error
}
});
我收到以下错误:
src/routes/secured-api.ts:38:21 - error TS2339: Property 'user' does not exist on type 'Request'.
38 req.user = user;
~~~~
我的User
类是:
import { Model, RelationMappings } from 'objection';
export class User extends Model {
public static tableName = 'User';
public static idColumn = 'username';
public static jsonSchema = {
type: 'object',
required: ['fname', 'lname', 'username', 'email', 'password'],
properties: {
fname: { type: 'string', minLength: 1, maxLength: 30 },
lname: { type: 'string', minLength: 1, maxLength: 30 },
username: { type: 'string', minLength: 1, maxLength: 20 },
email: { type: 'string', minLength: 1, maxLength: 320 },
password: { type: 'string', minLength: 1, maxLength: 128 },
}
};
public static modelPaths = [__dirname];
public static relationMappings: RelationMappings = {
};
public fname!: string;
public lname!: string;
public username!: string;
public email!: string;
public password!: string;
}
我的tsconfig.json
是:
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es2015"], /* Specify library files to be included in the compilation. */
"outDir": "./build", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}
我的目录结构是:
backend/
package.json
tsconfig.json
express.d.ts
src/
models/
user.ts
routes/
secured-api.ts
我在做什么错了?
推荐答案
问题是您没有在使用express
定义的Express
全局命名空间进行扩展,而是在模块中创建了一个新的命名空间(文件变为使用import
).
The problem is that you are not augmenting the Express
global namespace defined by express
you are creating a new namespace in your module (the file becomes a module once you use an import
).
解决方案是在global
import { User } from "./src/models/user";
declare global {
namespace Express {
export interface Request {
user: User;
}
}
}
或者不使用模块导入语法,只需引用类型:
Or not use the module import syntax, just reference the type:
declare namespace Express {
export interface Request {
user: import("./src/models/user").User;
}
}
这篇关于无法在TypeScript中扩展Express Request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!