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

202
Vistas
How to set the response type using axios with Javascript and JSDocs?

My API has a response as follows:

{
  "foo": "bar",
  "bar": "foo"
}

So I've created the response type using JSDocs as follows:

/**
 * typedef {Object} AwesomeAPIResponse
 * property {string} foo
 * property {string} bar
 */

So, I'm calling my API using Axios with a post request:

const axios = require('axios').default

const someFunction = async () => {
  const result = await axios.post(MY_API_URL, data, {
    headers: { ... },
    method: 'post'
  })

  return result
}

So far, so good, but the type of my response is AxiosResponse<any>, and I want it to be AxiosResponse<AwesomeAPIResponse>. I know I can achieve this by using TypeScript generics, but I'm not allowed to use TS and I don't want to return any as type. Is it possible to do something like this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

JSDoc is just used for type hinting for the IDE it can't be used in the way you're trying to.

The only way to return a "AwesomeAPIResponse" is by creating a class with that definition

class AwesomeAPIResponse {
    constructor(foo, bar) {
        this.foo = foo;
        this.bar = bar;
    }
}

and then returning a new instance of it based on the result

const someFunction = async () => {
  const result = await axios.post(MY_API_URL, data, {
    headers: { ... },
    method: 'post'
  })
  const {foo, bar} = result;

  return new AwesomeAPIRespone(foo, bar);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

This works in VSCode.

/**
 * @typedef {Object} Comment
 * @property {number} id The PK.
 * @property {number} postId The FK of the post the comment is related to.
 * @property {string} body The comment's content.
 */

/**
 * @typedef {import("axios").AxiosResponse<Comment>} CommentResponse
 */

/**
 *
 * @param {number} id The primary key of the comment to find.
 * @returns {Promise<CommentResponse>}
 */
async function findCommentById(id) {
  return axios.get(`https://jsonplaceholder.typicode.com/comments/${id}`);
}

const commentRes = await findCommentById(5);
console.log(commentRes.data.body);

axios response data intellisense

axios response intellisense

Warning: Trying to avoid having to import each time by doing this did not work: @typedef {import("axios").AxiosResponse} AxiosRes -> AxiosRes<YourDataType>


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