by com2kid · 3y ago · 0 comments
My go to example being a function that takes in an object with a bunch of camel case parameterFields and makes them into snake case parameter_fields.
Easy peasy in TypeScript, You can say, in a generic fashion, "pass an object with camelCase field names, and return the same object but the field names are snake_case".
This is incredibly useful when adapting between different systems!
Traditional OO systems fall flat on their face attempting such feats.
Sadly, not contrived!
Anyone using protocol buffers is actually familiar with this exact scenario. Not sure how the JS PB compiler handles things though, I never bothered to look under the covers.
Here is a random sample I found of both the types and the conversion code.
https://gist.github.com/alfrdmalr/faa012e421d9f7f60c3e6d2d32...
https://gist.github.com/alfrdmalr/faa012e421d9f7f60c3e6d2d32...
IIRC in one of the "what's new announcements" from the TS team included sample code to do it as well.
Edit: here is another example of doing it https://gist.github.com/kuroski/9a7ae8e5e5c9e22985364d1ddbf3...
And here is a medium article walking through the code step by step https://medium.com/@fullstack-shepherd/typescript-transformi...