Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js/Typescript: Error TS2339: Property 'send' does not exist on type 'Response'

I'm seeing this in Phpstorm 2019.3 with a red lint under .send()

// package.json
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },

// index.ts
const express = require("express")();

express.get('/', (req: Request, res: Response): void => {
 res.send('Express App Running') // TS2339: Property 'send' does not exist on type 'Response'
});

Are these the wrong type definitions or is something else at play?

like image 235
Sean D Avatar asked Nov 29 '19 21:11

Sean D


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Does not exist on type request?

The "Property does not exist on type Request" error occurs when we access a property that does not exist in the Request interface. To solve the error, extend the Request interface in a . d. ts file adding the property you intend to access on the request object.


1 Answers

The Request and Response types in the callback are probably not the ones provided by Express.
Make sure you import the right types:

import express, { Request, Response } from "express";

const app = express();

app.get("/", (req: Request, res: Response) => {
  res.send("foo");
});
like image 70
Mate Solymosi Avatar answered Sep 21 '22 13:09

Mate Solymosi