typescript - 如何使用类型推断正确地为`express.Response`进行声明合并
问题描述
我正在尝试更改声明Response.locals
以实现额外的类型安全性。在回答了类似问题之后,我在我的项目中添加了以下内容index.d.ts
:
import * as express from "express";
interface Locals {
someData: string
}
declare module "express" {
export interface Response {
locals: Locals;
}
}
如下使用时效果很好:
app.post("/somePath", (req, res: express.Response) => {
// res.locals has type `Locals` here
});
但是,如果我省略显式说明 的类型,编译res
器似乎会推断出不同的类型Response
Response.locals
any
app.post("/somePath", (req, res) => {
// oops, res.locals has type `any` here
});
我最初想通过使用 Typescript 来避免拼写错误(比如somedata
代替)。someData
Response
我对界面的扩充有什么问题?
解决方案
推荐阅读
- html - 播放 Youtube 视频时暂停 Bootstrap 轮播
- vba - 在 vba 中未检测到方程和图像
- perl - 如何在 Perl 中保存哈希的第二维
- javascript - 如何从句子中挑选攻击性词或与攻击性词混合的句子
- javascript - React - 无法从状态访问速率(api调用)
- ruby-on-rails - has_one/has_many rails 与备用源 ID 的关联,而不是主键
- javascript - 使用 twitter api 获取令牌
- r - R:基于表格连接文本中的两个单词
- styled-components - 样式化组件:覆盖样式无法与服务器端渲染一起正常工作
- php - TYPO3 PHP 更新到 7.0 | 日志说“切换语句可能只包含一个默认子句”