gpt4 book ai didi

node.js - 无法在 TypeScript 中扩展 Express Request

转载 作者:搜寻专家 更新时间:2023-10-30 21:05:08 25 4
gpt4 key购买 nike

这个问题已经被问过好几次了,但是没有一个答案对我有用。我正在尝试扩展 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 全局命名空间您正在模块中创建一个新的命名空间(一旦您使用该文件就成为一个模块导入)。

解决方法是在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;
}
}

关于node.js - 无法在 TypeScript 中扩展 Express Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com