Question: Imagine that I have a function that behaves differently when passed one parameters vs two. For example:

```js
function test(a: string | number, b?: number ) {
  if(typeof b !== 'undefined' && typeof a === "number"){
    return a * b
  }
  if (typeof a === "string" && typeof b === "undefined"){
    return a.toUpperCase() 
  }
  throw new Error("Incorrect Params");
}

```

**Function behavior**:

* When only `a` is present and it is a string, convert it to uppercase.
* When both `a` and `b` are present, both must be numbers, and multiply them.
* Any other type of combination throws an error

---

The question is, can it be typed in such a way that this behavior is reflected in the IDE?

That is, when only one parameter is passed to it, the IDE will show this

[![Function one parameter](https://i.stack.imgur.com/4jRdc.png)](https://i.stack.imgur.com/4jRdc.png)

But when passed two, or a comma is added, the behavior changes to show this:

[![Function two parameters](https://i.stack.imgur.com/0EClg.png)](https://i.stack.imgur.com/0EClg.png)

This would allow to avoid error during code development.

---

**Clarification**: The exposed function is only an example and not a real use case. Still I think this would be very useful and I have seen it implemented in the [Google Apps Script](https://developers.google.com/apps-script) IDE. I would like to know what the internal mechanism is, and if it can be extended to VSCode using only Typescript.

---

Answer: 
You're after [function overloading](https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads), which in your case would be implemented like so:

```
function test(a: number, b: number): number;
function test(a: string): string;

function test(...args: any[]): string | number {
  // implementation based on args
}

```

Which presents like this in your IDE - notice the 1/2 to denote the possible overloads, which you can cycle though:

[![enter image description here](https://i.stack.imgur.com/KKZcU.png)](https://i.stack.imgur.com/KKZcU.png)
After reviewing the marked as duplicated question a possible solution will be (not the most optimal because I have to change the behavior of the function):

```js
function test(...args: [a: string] | [a: number, b: number]): number | string {
  let a = args[0];
  let b = args[1];
  if (typeof b !== "undefined" && typeof a === "number") {
    return a * b;
  }
  if (typeof a === "string" && typeof b === "undefined") {
    return a.toUpperCase();
  }
  throw new Error("Incorrect Params");
}

```