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

120
Vistas
Typescript how to return key type

I can’t find information on how to correctly return exactly the type of a certain property of the type, passing the key in another field.

Can someone tell me or point out what I'm doing wrong? :(

type TUser = {
    age: number,
    initials: {
        name: string
    }
}

type TRendererFunctionArgs<TValues> = {
    value: TValues[keyof TValues],
}

type TRenderer<TValues> = {
    name: keyof TValues,
    renderer: ({ value }: TRendererFunctionArgs<TValues>) => string,
}

const renderer : TRenderer<TUser> = {
    name: 'initials',
    renderer: ({ value }) => value.name, // <--- There is problem because. value : value: number | {name: string}
}

Playground link

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Here's an approach that uses a mapped type as an indexed access type:

type TUser = {
    age: number,
    initials: {
        name: string
    }
}

type TRenderer<TValues> = { [K in keyof TValues]-?: {
    name: K;
    renderer: ({ value }: { value: TValues[K] }) => string;
} }[keyof TValues];

const renderer : TRenderer<TUser> = {
    name: 'initials',
    renderer: ({ value }) => value.name
}
about 4 years ago · Santiago Gelvez 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