TypeScript: The problem with function overloads
Briefly

Similarly to function overloads, the return type is properly inferred according to the input value: const randomString: string = random("string");const randomNumber: number = random("number");
Unlike function overloads, uncertain input values are allowed and propagate to the returned type. Still, using 'any' may be required: const randomValue: string | number = random(mode as "string" | "number");
TypeScript struggles to infer return types without introducing 'any'. Using type aliases provides cleanliness, but adherence to return constraints might not be guaranteed.
Read at Medium
[
|
]