Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
How to Type a request parameter with express server

How do I Type (typescript) the attached post request to fix the error? I want to get the request body, but I can't type it properly.

Thanks!

enter image description here

import express = require('express');
import { Request } from 'express';
import bodyParser from 'body-parser';
import { parseBMI, calculateBMI } from './bmiCalculator';
import { calculateExercises } from './exerciseCalculator';

const app = express();
app.use(bodyParser.json());

app.get('/hello', (_,res) => {
  res.send("Good day");
});

app.get('/bmi', (req,res) => {
  const weight = Number(req.query.weight);
  const height = Number(req.query.height);
  console.log(weight,height);
  try {
    const {parseHeight, parseWeight} = parseBMI(height,weight);
    const out: string = calculateBMI(parseHeight,parseWeight);
    res.json({
      weight:parseWeight,
      height:parseHeight,
      bmi:out
    });
  } catch (e) {
    res.status(4004).json(e);
  }

});
app.post('/exercises',(req: Request<Array<number>,number>,res) => {
    const body:any = req.body;
    const dailyExercises = body.daily_exercises as Array<number>;
    const target = Number(body.target);

    res.json(calculateExercises(dailyExercises,target));
  });

const PORT = 3003;

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

this is only concerning the /exercises route which throws error with eslint plugin on vscode

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The out of the box express types support generic parameters, so you can simply pass the type in the signature:

app.post('/foo', async (req: Request<any, any, {bar: string}, any>, res: Response) => { 
  const x = req.body.bar; //string
}

However, it's worth nothing that this does not perform any validation and is effectively the same thing as the as any cast. A better approach would be to use a library like io-ts or yup to do a validation check. That way you actually know object is in the correct form.

about 4 years ago · Juan Pablo Isaza Report

0

You will want to define an interface for your request:

interface Exercise {
    dailyExercises: number[],
    target: number
}

const exercise = req.body as Exercise

By then casting your req.body to an Exercise, you have an exercise constant that is strictly typed.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!