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

159
Views
Tipo de escritura a máquina para el objeto de evento de cambio de window.matchMedia

Escucho los cambios de window.matchMedia

Tengo este error: Parameter 'event' implicitly has an 'any' type.

¿Qué tipo de TS debo especificar aquí para el objeto de event ?

 const mobileMediaQuery = window.matchMedia("(max-width: 699px)"); mobileMediaQuery?.addEventListener("change", processViewSizeChange); function processViewSizeChange(event) { console.log('media query changed! →', event.matches); }

¿dónde en Internet para buscar tipos globales mecanografiados? en caso de que necesite encontrar algo similar en el futuro.

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

0

Puede usar TypeScript para obtener información sobre los tipos que necesita. Aquí tienes un ejemplo en tu caso. Sabe que necesita proporcionar el tipo de evento de "change" y que necesita proporcionar una función de devolución de llamada para el oyente, pero no está seguro de cómo escribir la función de devolución de llamada, así que déjelo fuera al principio:

 const mobileMediaQuery = window.matchMedia('(max-width: 699px)'); mobileMediaQuery.addEventListener('change', ); /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected 2-3 arguments, but got 1.(2554) */

Si no incluye el segundo argumento de mobileMediaQuery.addEventListener , obtendrá un diagnóstico de error del compilador como ese.

Pero si está utilizando un IDE que incluye TypeScript Language Server y algo como IntelliSense (por ejemplo, VS Code o TypeScript Playground ), puede pasar el mouse sobre el método addEventListener para ver su firma:

 (method) MediaQueryList.addEventListener<"change">(type: "change", listener: (this: MediaQueryList, ev: MediaQueryListEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void (+1 overload)

Como puede ver en la firma, el listener es el segundo argumento y el tipo del parámetro de evento en el oyente es MediaQueryListEvent . Entonces puedes escribir tu función de oyente así:

 function processViewSizeChange(event: MediaQueryListEvent): void { console.log('media query changed! →', event.matches); }

Código final en TS Playground

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!