Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda