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

392
Views
Devolver un objeto de una función y pasar la referencia a un accesorio da mi error de TypeScript. No sucede si el objeto se coloca directamente en prop.

Yo estoy haciendo esto:

 const getDataForChart = () => { const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July']; const test = { labels, datasets: [ { label: 'Dataset 1', data: [0, 5, 10, 15], borderColor: 'rgb(255, 99, 132)', backgroundColor: 'rgba(255, 99, 132, 0.5)', }, { label: 'Dataset 2', data: [2, 7, 12, 17], borderColor: 'rgb(53, 162, 235)', backgroundColor: 'rgba(53, 162, 235, 0.5)', }, ], }; return test; };

Y luego agregar la referencia de la función en los accesorios de datos del gráfico de líneas de esta manera:

 <Line data={getDataForChart} options={options} width={400} height={400} />

Resultados en el siguiente error:

Property 'datasets' is missing in type '() => { labels: any; datasets: any; }' but required in type 'ChartData<"line", (number | ScatterDataPoint | null)[], unknown>'.

Pero si paso el objeto literal directamente en el accesorio, no obtengo el error y el gráfico se representa correctamente.

 <Line data={{ labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { label: 'Dataset 1', data: [0, 5, 10, 15], borderColor: 'rgb(255, 99, 132)', backgroundColor: 'rgba(255, 99, 132, 0.5)', }, { label: 'Dataset 2', data: [2, 7, 12, 17], borderColor: 'rgb(53, 162, 235)', backgroundColor: 'rgba(53, 162, 235, 0.5)', }, ], }} options={options} width={400} height={400} />

¿Por qué está pasando esto?

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

0

Tienes que llamar a la función.

 <Line data={getDataForChart()} options={options} width={400} height={400} />
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!