Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

97
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

7 months 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 }
7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.