Kevin Kreuzer
1 min readOct 28, 2018

--

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.

--

--

Kevin Kreuzer

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