Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
TypeScript Element implicitly has an 'any'

I have some issues with TypeScript. I'm trying to transform some value that is coming from parent component to child. Parent is sending value that can be EN, FR or NL, and those values I should transform to numbers, so I did that:

const transform_translation = {
      'EN': 1,
      'FR': 2,
      'NL': 3
    };

But when I try to get value like transform_translation[props.language], It says;

S7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ EN: number; FR: number; NL:

Props:

props: {
    language: {
      type: String,
      default: ['EN', 'NL', 'FR'],
      required: true
    },
.
.
.
}

How should I define them then? I work first time with TS btw.

number; }'.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should tell ts what you're passing is not just string but limited to the available keys of the languages you have:

const transform_translation = {
  EN: 1,
  FR: 2,
  NL: 3
};

// create type out of valid keys of object
type Language = keyof typeof transform_translation;

const getLanguageNum = (lang: Language) => transform_translation[lang];

console.log(getLanguageNum("EN")); // 1
about 4 years ago · Juan Pablo Isaza Denunciar

0

I would either create an enum or type for the key like so. Then when accessing it you can cast the the key into the type as follows

type ITransformTranslationKey = 'EN' | 'FR' | 'NL';
const transform_translation: Record<ITransformTranslationKey, number> = {
      'EN': 1,
      'FR': 2,
      'NL': 3
    };

const someVar: ITransformTranslationKey = 'E' + 'N'; // you can define the type here
transform_translation[someVar as ITransformTranslationKey] // or here
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda