小编Dan*_*ier的帖子

合并接口时覆盖TypeScript中的`any`

我正在使用 Express 并且我正在尝试明确定义res.locals. 在@types/express 包中, Express.Response.locals 是any,所以我似乎无法覆盖它:

类型/快递/index.d.ts:

declare namespace Express {
  interface Response {
    locals: {
      myVar: number
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的中间件:

import * as express from 'express'

function middleware(
  req: express.Request, 
  res: express.Response, 
  next: express.nextFunction
) {
  res.locals.myVar = '10' // I want this to throw a compiler error
  next()
}
Run Code Online (Sandbox Code Playgroud)

我希望我的错误分配res.locals.myVar为错误,但res.locals仍然any根据我的自动完成。

我怎样才能删除any并完全替换它?

node.js express typescript

8
推荐指数
1
解决办法
1715
查看次数

标签 统计

express ×1

node.js ×1

typescript ×1