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

407
Vistas
Typescript - How to return dynamic "object key" that comes from dynamic "prop key value"?

I'm trying to generate "dynamic object key" based of "prop key value" by picking specific object key value.

So let's say I pass in { name: 'someEntity', unrelatedKey: 123 } and I would like to return { someEntity: 'ok' }. How would one do that so the return value is typescript type defined? I've tried something like this but it does not work:

type GetExampleProps = {
  name: string
  unrelatedKey: number
}

type GetExampleRes<T> = {
  [key: T]: string
}

function getExample<T extends GetExampleProps> (props: T): GetExampleRes<T['name']> {
  return {
    [props.name]: 'ok'
  }
}

const example = getExample({ name: 'someEntity', unrelatedKey: 123 })

example.someEntity  // should be valid type string
example.hello       // should be invalid, missing key
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Currently I am unable to do that without a type assertion, but as a temporary solution here it is:

type GetExampleProps<T extends PropertyKey = string> = {
  name: T;
  unrelatedKey: number;
};

function getExample<T extends PropertyKey>(
  props: GetExampleProps<T>
): {
  [K in T]: string;
} {
  const toRet = {
    [props.name]: "a string",
  };

  return toRet as { [K in T]: string };
}

const example = getExample({ name: "someEntity", unrelatedKey: 123 });

example.someEntity;
example.hello; // Property 'hello' does not exist on type '{ someEntity: string; }'

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this function, this should work the way you need

const createObject = (obj: any) => {
    interface I {
        [name: string]: any;
    }
    const returnObj: I = {};
    returnObj[obj.name] = Object.keys(obj).filter((key) => key !== "name");
    return returnObj;
};
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