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

210
Views
¿Cómo configurar el tipo de respuesta usando axios con Javascript y JSDocs?

Mi API tiene una respuesta de la siguiente manera:

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

Así que creé el tipo de respuesta usando JSDocs de la siguiente manera:

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

Entonces, estoy llamando a mi API usando Axios con una solicitud posterior:

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

Hasta ahora todo bien, pero el tipo de mi respuesta es AxiosResponse<any> y quiero que sea AxiosResponse<AwesomeAPIResponse> . Sé que puedo lograr esto usando genéricos de TypeScript, pero no puedo usar TS y no quiero devolver any como tipo. ¿Es posible hacer algo como esto?

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

0

JSDoc solo se usa para sugerencias de tipo para el IDE, no se puede usar de la forma en que lo intenta.

La única forma de devolver una "AwesomeAPIResponse" es creando una clase con esa definición

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

y luego devolver una nueva instancia basada en el resultado

 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 Report

0

Esto funciona en 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);

intellisense de datos de respuesta de axios

inteligencia de respuesta de axios

Advertencia: Intentar evitar tener que import cada vez haciendo esto no funcionó: @typedef {import("axios").AxiosResponse} AxiosRes -> AxiosRes<YourDataType>


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!