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

146
Visualizações
How to type hint specific objects in function argument

Suppose I have some function:

const someFunc = (input) => {// do something}

Where I know the input is an object that will have the property thing, ie. in the function I might check what input.thing is. How do I type hint the function when I declare it? I tried something like:

const someFunc = (input: { thing: string }) => {// do something}

But that doesn't seem to work.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

That works just fine. You are declaring the first argument is an object with thing as a required string.

https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBFMBbApgMQK4RHAvHACgEsIAHDGALjgG84YALEgc2tgCcW4BfASjwB8tYHFFxw0MABsUAOilhmxMhVmMWvANzBuwRKkzYCNdRFZwA5AxRSFFvsCA

const someFunc = (input: { thing: string }) => {
}

It can be helpful to work it out with a type instead of inline, just because inline types look kind of like destructuring.

type Input = {
    thing: string
}
const someFunc = (input: Input) => {}

You might want to allow other keys, but require thing in which case you can add [key: string]: any to your object.

about 4 years ago · Santiago Gelvez 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