【问题标题】:How to extend express Request type in a ts-node project?如何在 ts-node 项目中扩展 express Request 类型?
【发布时间】:2019-05-14 21:47:52
【问题描述】:

我试过thisthis

// ./typings/express/index.d.ts
declare namespace Express {
  export interface Request {
     token?: string
  }
}

示例用法:

import * as express from 'express'

(req: express.Request, res: express.Response, next: express.NextFunction) => {

  const foo = req.token

}

如果我直接编译 (tsc -p .),它确实可以工作,它在 Visual Code 中也可以工作,但是当我尝试使用 ts-node 运行时,我总是得到:

错误 TS2339:“请求”类型上不存在属性“令牌”。

知道如何让它与 ts-node 一起使用吗?

版本:ts-node@7.0.1typescript@3.0.1

【问题讨论】:

  • 你在哪里定义的?
  • ./typings/express/index.d.ts,我也试过有无typeRoots

标签: typescript ts-node


【解决方案1】:

对我有用的唯一解决方法是启用文件标志

ts-node --files index.ts

对于摩卡咖啡

TS_NODE_FILES=true mocha

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2021-05-30
    • 2022-11-04
    • 2020-01-11
    • 2015-11-05
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多