Node.js+TypeScript中res.status()报错:类型Number无调用签名
Node.js + TypeScript中res.status(500)报错:Type 'Number' has no call signatures的解决方法
问题重现
我在搭建Node.js与TypeScript开发环境时遇到基础问题,编写的路由控制器代码如下:
public allUsers = (req: Request, res: Response) => { res.status(500).json({ status: "ERROR", message: "Not implemented", }); };
其中res.status(500)触发TypeScript错误:
This expression is not callable.Type 'Number' has no call signatures.ts(2349)
解决方法
这个问题本质是TypeScript没有正确识别express.Response类型,导致把status当成了普通的Number属性而非链式调用方法,按以下步骤排查修复:
安装express类型定义包
确保项目中已安装@types/express,这是TypeScript识别express API类型的关键依赖:npm install @types/express --save-dev # 或使用yarn yarn add @types/express -D检查Request/Response的导入来源
必须从express模块显式导入类型,不要依赖全局类型(严格模式下全局类型可能不生效):import { Request, Response } from 'express';排查自定义类型冲突
如果项目中有自定义的Response接口扩展,确保没有错误覆盖status属性的定义。比如不要出现类似以下的错误定义:// 错误示例:覆盖了status的方法类型 interface Response { status: number; }
内容的提问来源于stack exchange,提问作者shivetay
相关产品推荐
相关产品推荐

