TypeScript Class with Optional Parameters and Default Values - Confusion with Method Overloading
I've been banging my head against this for hours. I'm deploying to production and I'm updating my dependencies and I'm working with an scenario with method overloading in my TypeScript class that uses optional parameters with default values. I have a class `User` where I want to create a method `setProfile` that accepts a `name` and an optional `age`. I want to overload this method to handle different combinations of parameters. However, the TypeScript compiler is throwing an behavior when I try to call the method with no arguments. Here's a simplified version of my code: ```typescript class User { name: string; age?: number; constructor(name: string, age?: number) { this.name = name; this.age = age; } setProfile(name: string): void; setProfile(name: string, age: number): void; setProfile(name?: string, age?: number): void { this.name = name || this.name; if (age !== undefined) { this.age = age; } } } const user = new User('Alice', 25); user.setProfile(); // behavior: Expected 1-2 arguments, but got 0. ``` The behavior message states that the method expects 1 to 2 arguments, but I'm calling it without any. I thought that since I provided default values, it should work. I tried using optional chaining and providing default values in the method signature, but that didn't resolve the scenario. Am I misunderstanding how method overloading works with optional parameters in TypeScript? How can I adjust my method implementation to allow a call without any arguments while maintaining the overload signatures? This is part of a larger application I'm building. Any ideas what could be causing this? My development environment is Windows. Is there a better approach? For context: I'm using Typescript on Ubuntu 20.04. This is part of a larger service I'm building. What's the best practice here? I've been using Typescript for about a year now. Any advice would be much appreciated. This is happening in both development and production on macOS. Any examples would be super helpful.