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

148
Vistas
React + Typescript: How can I dynamically set a generic interface for a function?

Consider the following components:

Basic.tsx

type BasicPropertyDetails<T extends unknown> = T & {
  label: string
  id: string
}

interface Props {
  properties: BasicPropertyDetails<unknown>[]
  onPropertySelect: <T>(property: BasicPropertyDetails<T>) => void
}

const Basic = ({ properties, onPropertySelect }: Props) => { ... }

Extended.tsx

export interface ExtendedPropertyDetails{
  status: string
  price: string
}

export interface Props {
  ExtraProperties: BasicPropertyDetails<ExtendedPropertyDetails>[]
  onPropertySelect: (property: BasicPropertyDetails<ExtendedPropertyDetails>) => void
}

const Extended = ({ ExtraProperties, onPropertySelect }: Props) => { 
  ...

  return (
    <Basic properties={ExtraProperties} onPropertySelect={onPropertySelect} />
  )
}

I need the onPropertySelect's (in Basic.tsx) type to equal whatever type is passed into it in Extended.tsx. i.e. onPropertySelect in Basic should know the type to use is (property: BasicPropertyDetails<ExtendedPropertyDetails>) => void (based on the value in Extended.tsx).

Note: The type of property inside onPropertySelect will have the same subtype as BasicPropertyDetails<unknown>

As of now I cannot get the above to work and the implementation (or the return tsx) in Extended throws an error for types not matching.

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

0

In Extended.tsx, pass type T to onPropertySelect property.

export interface Props {
  ExtraProperties: BasicPropertyDetails<ExtendedPropertyDetails>[]
  onPropertySelect: <ExtendedPropertyDetails>(property: BasicPropertyDetails<ExtendedPropertyDetails>) => void
}
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