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

107
Views
How can I specify types on a camel to snake case conversion function

I have a function that converts the case of a target string, array, or object from camel case to snake case.

convertCamelToSnake (target) {
  // Complex logic to convert target depending on what type it is
  return convertedTarget
}

I want to specify types on this function using a Generic like so:

convertCamelToSnake <T>(target: T):T {
  // Complex logic to convert target depending on what type it is
  return convertedTarget
}

This works for strings and arrays, but we convert the keys of a given object to snake case. But typescript doesn't realize this and assumes that the keys remain the same because we declare that we are returning T.

How can I retain the ability to know the type that is returned. I.e. string, array, object, while leaving the keys of the dictionary ambiguous?

For instance let's say we pass in:

const a = { helloWorld: "bob", age: 21 }
a = convertCamelToSnake(a); // { hello_world: "bob", age: 21 };
a.hello_world; // Property 'hello_world' does not exist on type 

How can we make it so we're allowed to access the hello_world property after converting the object, while also still being able to infer that the return value is of type object (since we could for instance also be passing in an array into this function)?

about 4 years ago · Juan Pablo Isaza
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!