controller.ts(8,8):错误TS2339:属性'用户'类型'请求'上不存在同时扩展express.Request

时间:2018-04-18 15:04:13

标签: typescript typescript-typings

我收到以下错误。请帮帮我。

controller.ts(8,8): error TS2339: Property 'user' does not exist on type 'Request'.

controller.ts

import * as express from 'express';
class Users{
 public showUser(
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ) {
     return req.user;

 }
}
const users = new Users();
export = {
 showUser: users.showUser
}

express.custommerge.d.ts

// Extend the Express interface with declaration merging
import { User } from './api';
declare namespace Express {
  export interface Request {
    user: User; 
  }
}

api.ts

export class User {
 'userId': string
}

注意:

如果我在express.custommerge.d.ts中添加import { User } from './api';并添加any而不是User

,则不会出现此错误

1 个答案:

答案 0 :(得分:0)

如果我在

中进行以下更改,则有效

express.custommerge.d.ts

HashTable