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

407
Views
La propiedad no existe en TypeOf, entendiendo mejor las clases

He estado jugando con clases de aprendizaje y TypeScript, y encontré un error que no entiendo; espero que alguien pueda ayudarme.

He creado la siguiente interfaz:

 export default interface IServerApi { url: string call: string }

Y luego creo una clase que debería hacer mis apicales así:

 class ApiCall implements IServerApi { public url: string public call: string constructor(apiUrl: string, apiEndpoint: string) { this.url = process.env.REACT_APP_SERVER_URL as string this.call = "products" } static async getAll() { const response = await fetch(`${this.url}/${this.call}`) const data = await response.json() return data } }

Sin embargo, aparece un error de TS debajo de this.url con el siguiente mensaje:

 any Property 'url' does not exist on type 'typeof ApiCall'.ts(2339)

¿Alguien puede indicarme qué está pasando aquí? Pensé que this.url haría referencia a la URL establecida en el constructor, y luego podría pasar algo como this.url a los métodos. El objetivo sería simplemente poder llamar a ApiCall.getAll() en lugar de volver a escribir una búsqueda.

¡Gracias!

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

0

El problema está relacionado con una llamada de método de estado, el método getAll es estático y los métodos estáticos no tienen esta referencia.

Hay 3 soluciones para esto

  1. Eliminar estática de getAll async getAll()
  2. Agregue una palabra clave estática en la URL. public static url
  3. Cree una instancia de la clase actual y use
 const instance = new ApiCall("", ""); const response = await fetch(`${instance.url}/${this.call}`);

Recomendado para su problema:

 export default interface IServerApi { url: string call: string } class ApiCall implements IServerApi { public static url: string = process.env.REACT_APP_SERVER_URL as string public call: string constructor(apiUrl: string, apiEndpoint: string) { this.call = "products" } static async getAll() { const response = await fetch(`${ApiCall.url}/${this.call}`) const data = await response.json() return data } }
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!