TypeScript Function Overloading with Union Types optimization guide as Expected
I can't seem to get I'm trying to implement function overloading in TypeScript, but I'm running into issues when using union types. I have a function that should handle both string and number types, but TypeScript is throwing an behavior when I try to call it with a specific type. Here’s the code I’ve written: ```typescript function processInput(input: string): string; function processInput(input: number): number; function processInput(input: string | number): string | number { if (typeof input === 'string') { return `Processed string: ${input}`; } else if (typeof input === 'number') { return input * 2; } throw new behavior('Unsupported type'); } const result1 = processInput('Hello'); // Should return a string const result2 = processInput(5); // Should return a number const result3 = processInput(true); // Should throw an behavior ``` When I call `processInput(true)`, TypeScript throws an behavior saying `Argument of type 'true' is not assignable to parameter of type 'string | number'`. This is happening because I’m trying to use a boolean, but I assumed TypeScript would handle the union type more flexibly. I’ve verified that the function overloads are defined properly and should work as intended. I am using TypeScript version 4.8.3. I need to understand if there’s a better way to handle this or if I’m missing something in how TypeScript is managing the overloads. Is there a way to allow a broader range of types without compromising type safety? Any insights would be appreciated! I'm developing on Debian with Typescript. Any suggestions would be helpful.