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

360
Vistas
How to type link prop in component (React + TypeScript)?

I'm fairly new to TypeScript and I've been struggling with this for a bit now.

I would like to pass on routes to a component as a prop, but I don't know how to type it, so I keep getting TypeErrors from TypeScript.

ParentComponent (where I pass on the routes):

import { ChildComponent } from '../components';

export const ParentComponent = () => (
  
  <div>
    ...
    <ChildComponent to={"/page_one"}>text</ChildComponent>
    <ChildComponent to={"/page_two"}>text</ChildComponent>
    <ChildComponent to={"/page_three"}>text</ChildComponent>
    ...
  </div>
)

And then my child component looks like this:


  import { Link } from 'react-router-dom';

  interface ChildProps {
  children: React.ReactNode
  to?: React.LocationDescriptor<any> 
  }

  export const ChildComponent = ({children, to}): ChildProps => {
   
    ...
    <Link to={to}/>
    {children}
    ...

  }

I keep getting the Error: "... is not assignable to type 'LocationDescriptor | ((location: Location) => LocationDescriptor)'."

I have tried multiple ways to type it and have searched around, but couldn't find any solution that worked for me.

So, what's the correct way to type the 'to' prop?

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

0

As far as I know keeping the type as string would solve the matter

type Props = { to: string; };

Also remove the question mark to make sure that the value is always passed. For some reason if the value you might get can be undefined, add a check or default value to it.

Hope this answers your query.

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