这个问题已被多次询问,但所给出的答案都没有对我有用。我正在尝试扩展Express Request
对象以包含存储User
对象的属性。我创建了一个声明文件express.d.ts
,并将其放在与我的tsconfig.json
相同的目录中:
import { User } from "./src/models/user";
declare namespace Express {
export interface Request {
user: User;
}
}
然后我尝试在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
,该文件就变成了一个模块)。
解决方案是在global
中声明命名空间
import { User } from "./src/models/user";
declare global {
namespace Express {
export interface Request {
user: User;
}
}
}
或者不使用模块导入语法,只需引用类型:
declare namespace Express {
export interface Request {
user: import("./src/models/user").User;
}
}
我有同样的问题......
你不能只使用扩展请求吗?喜欢:
interface RequestWithUser extends Request {
user?: User;
}
router.post('something',(req: RequestWithUser, res: Response, next)=>{
const user = req.user;
...
}
另一方面,如果您使用快速异步回调,请确保用户使用快速异步包装器,或确保您确切知道自己在做什么。我建议:awaitjs
0
声明合并意味着编译器将使用相同名称声明的两个单独声明合并到一个定义中。
请参阅以下示例,其中接口声明在typescript中合并
interface Boy {
height: number;
weight: number;
}
interface Boy {
mark: number;
}
let boy: Boy = {height: 6 , weight: 50, mark: 50};