• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

168
Vistas
File importing with Typescript

I'm importing the file, but I can't give the type of the file, how can I solve this problem?

import { capitalize } from "utils";

import * as Icons from "./";
type IconType = {
  name: string;
};
const icon = ({ name, ...props }: IconType) => {
  const newName = capitalize(name);
  if (!Icons[newName]) return null;

  const CustomIcon = Icons[newName];
  return <CustomIcon {...props} />;
};

export default icon;

enter image description here

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can probably fix your issue by typing Icons[newName as keyof Icons].

You have a lot of bad practices in your code you should fix, though:

  1. You should add a import * as icons from "./pathToIcons";. Importing by the index.ts should only happen if importing from outside the same folder;
  2. const icon = (...) A component should start with capital letter, const Icon = (...);
  3. { name, ...props }: IconType This is incoherent, because your type IconType only has one attribute name, therefore in your case, typescript should infer props typeof never. Probably you want something interface IconProps extends CustomIconProps { name: string }
about 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda