You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Express+TypeScript中为res.json指定特定body类型的替代方案

解决Express中res.json()默认any类型替换为自定义类型的方案

最优方案:TypeScript模块扩展(Module Augmentation)

不用修改node_modules或依赖包的类型文件,直接通过TypeScript的模块扩展能力,全局重写Express的Response类型,让res.json()默认适配你的自定义JsendData接口,同时自动触发类型校验。

步骤1:创建自定义类型声明文件

在项目src目录下新建一个类型声明文件(比如express.d.ts),写入以下内容:

import 'express';

// 定义你的JsendData接口
interface JsendData {
  status: string;
  message: string;
  results: number;
}

// 扩展Express的Response类型
declare module 'express-serve-static-core' {
  interface Response<ResBody = JsendData, Locals extends Record<string, any> = Record<string, any>, StatusCode extends number = number> {
    json(body?: ResBody): this;
  }
}

步骤2:确保类型文件被TypeScript识别

检查tsconfig.json的include字段,确保包含这个声明文件,比如:

{
  "include": ["src/**/*", "src/express.d.ts"]
}

步骤3:验证类型校验效果

现在你的控制器代码会自动检测缺失属性的问题,比如:

import { RequestHandler } from 'express';

const sampleController: RequestHandler = (req, res) => {
  res.status(500).json({
    status: "error",
    message: "This route does not exist.",
    // TypeScript会直接报错:缺少'results'属性,不符合JsendData接口要求
  });
};

方案优势

  • 完全不需要修改node_modules里的文件,不会因为依赖更新丢失配置
  • 自动触发类型校验,比手动断言更可靠
  • 全局生效,所有路由的res.json都会默认遵循自定义类型

进阶:支持多场景响应类型

如果需要在某些场景下使用其他响应类型,可以把扩展改为泛型形式:

declare module 'express-serve-static-core' {
  interface Response<ResBody = any, Locals extends Record<string, any> = Record<string, any>, StatusCode extends number = number> {
    json<T = JsendData>(body?: T): this;
  }
}

这样既保留默认的JsendData类型,也支持临时指定其他类型:res.json<OtherResponseType>({ ... })

内容的提问来源于stack exchange,提问作者kasdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:12:18