此表达式不可调用。类型“号码”没有呼叫签名

Moh*_*mad 8 model node.js typescript

嗨,我是 Typescript 的新手,我有一个对象类型变量,其中可能是来自不同类型或嵌套对象的不同值。现在我的问题是,如何为这个对象定义一个模型,以便在调用不同的键时不会遇到示例错误?

例如:

export class Controller {
protected static response(res: Response, statusCode: number = 200, data: any, user: string = '', dev: string = '', code: number = 200, result: string = 'success'){
    res.status(statusCode).send({
        data: data,
        message: {
            user: '',
            dev: ''
        },
        code: 403,
        result: 'Error'
    })
}


 ERROR: res.status ---> This expression is not callable. Type 'Number' has no call signatures
Run Code Online (Sandbox Code Playgroud)

Hel*_*cos 16

在 NextJS 中,确保导入正确的响应类型。

import { NextApiResponse } from "next";
Run Code Online (Sandbox Code Playgroud)


小智 15

我也收到此错误,并意识到我刚刚忘记导入响应。添加导入行为我解决了这个问题。

import express, {Request, Response} from 'express';
Run Code Online (Sandbox Code Playgroud)

  • 为了上下文。默认情况下,打字稿会自动从 HTTP 节点库的核心导入它。在“Response”对象下的“status”有一个“Number”。 (3认同)