Greg Gum Thanks a lot for the response. You are right for the simple scenario described in the blog. The method signature just improves the intellisense. But you can absolutely do it without signatures and optional parameters. That’s up to you.

But there are other scenarios where the helper methods might be helpful. Imagine you have the following code:

public foo(value: number | Observable<number>, filter: (value: int) => boolean) {
// Some implementation
}

This function accepts a number or an Observable as first parameter and a filter function as second parameter. Having a filter function only makes sense when the first value is an Observable. With the code above we could easily call the function in the following way without Typescript complaining about it.

foo(1, (value) => value === 1);

Method signature allows us to express that we only allow a filter function in combination with an Observable. So if we add the following two signatures:

public foo(value: number);
public foo(value: Observable<number>, filter: (val: int) => boolean);

Typescript will now complain when we try to call foo with a number and a filter function. This allows you to avoid unnecessary runtime checks.

Written by

Passionate freelance frontend engineer. ❤️ Always eager to learn, share and expand knowledge.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store