As pointed out by @jcalz, the challenge lies in the fact that the type system is lost during the compilation of TS to JS.
To address this issue, there are a few potential solutions that can be explored:
- Develop two distinct functions with specific return types
- Introduce a flag within the existing function to indicate the desired call, resulting in a union type return
- Inject the desired function into the
doThing
function as an argument, leading to a union type return
In my scenario, I opted to create two separate functions with similar logic. Although this may seem less efficient, it improves readability and facilitates testing.