Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!