Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

333
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda