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

159
Vistas
Get value from object of unknown content by key in typescript

I'm writing function that'll return single value from object by key or return empty object if key does not exist.

Code:

//example data:
const obj = {
    data: {test: 'word'},
    str: 'Hi',
    num: 22,
}

const pickOne = <T extends object>(obj: T, key: keyof typeof obj) => ((key in obj) ? obj.key : {})

console.log(pickOne(obj, 'data')) //should print {test: 'word'}

My problem is that I get this error: Property 'key' does not exist on type 'T'. I'm new to typescript but after reading docs I was sure that keyof typeof would allow any key in obj.

Any help would be appreciated, thanks.

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

0

Your obj has 3 keys:

  • data
  • str
  • num

When you write obj.key you are telling the interpreter to find a key called key, which does not exist.

On the other hand you can access these keys by its name using brackets:

  • const key = "data"
  • obj[key] // Should return {test: 'word'} , which is equivalent to:
  • obj["data"] // Should return {test: 'word'}

Moral of the story:

  1. The dot-notation is to access existing keys in a given object
    • If the key exists it works. Otherwise, it errors out
  2. TypreScript also allows accessing these keys by name
    • If the key exists it works. Otherwise, doesn't error out but returns undefined

Working example:

const obj = {
    data: {test: 'word'},
    str: 'Hi',
    num: 22,
}

const key = "data"
console.log(obj[key])
console.log(obj["data"])
console.log(obj["key"])
about 4 years ago · Juan Pablo Isaza Denunciar

0

This:

obj.key

gets the property named "key" on obj, which may not exist.

Where this:

obj[key]

Gets the property name in the variable key.

Which works without type error: See playground

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