Consider a Java and TypeScript implementation of an
The class takes a list of numbers and adds each number by
In Java, we can pass a method into a stream with the
An analogous class in TypeScript looks like the this.
The Java implementation of
But if we run
we get an error that
this is undefined.
Luckily, the fix is simple: Wrap your function in a high-order function.
Now when we run our sample client,
this is defined in the private helper method and we don't get the error.