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

601
Views
errores ts(7053) y ts(2538) en Next.js + Ts

Busqué en Google y encontré los mismos problemas, pero como soy nuevo en TS, no pude implementarlo en mi caso. Asi que

En mi aplicación nextjs + ts , habilité la internalización para 4 idiomas. El contenido es estático y proviene del backend.


Esta es la forma de todo mi contenido estático.

 field: { en: "", ru: "", uz: "", oz: "" }

y esta es una demostración simplificada de cómo los muestro según el idioma del sitio:

 const static_content = { title:{ en: "Main title", ru: "Основное название", uz: "Bosh sahifa", oz: "Бош саҳифа" } ... } import {useRouter} from 'next/router' const router = useRouter() static_content.title[router.locale] // Type 'undefined' cannot be used as an index type. ts(2538)

Lo intenté:

 router.locale && static_content.title[router.locale]

pero en este caso dice:

 Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ en: string; ru: string; uz: string; oz: string; }'. No index signature with a parameter of type 'string' was found on type '{ en: string; ru: string; uz: string; oz: string; }'.ts(7053)


Los datos provienen del backend y se ven así:

 { title_en: "", title_ru: "", title_uz: "", title_oz: "", ... }

y así es como lo estoy mostrando

 interface dataType { title_en: string title_ru: string title_uz: string title_oz: string } const data: dataType = await fetchData() data['title_'+router.locale] // error ts(7053)

error ts(7053):

 Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'dataType'. No index signature with a parameter of type 'string' was found on type 'dataType'.ts(7053)

El proyecto ya estaba terminado en javascript, solo quería convertirlo a mecanografiado. Este es el único tipo de error que no pude resolver porque soy nuevo en ts y este único error existe en mis 20 páginas 2-4 por página, por lo que me gustaría tener una solución simple.

¡Gracias de antemano!

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

0

¿Podemos probar y declarar una interfaz para representar static content así...

 interface StaticContent { title: { [key: string]: string }; }

y luego asigne este tipo a los datos entrantes así...

 const static_content: StaticContent = { title: { en: "Main title", ru: "Основное название", uz: "Bosh sahifa", oz: "Бош саҳифа" } // ... }

y entonces podemos hacer fácilmente

 router.locale && static_content.title[router.locale]

¿Tenemos algún problema con este enfoque en su base de código?

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!