Anders
Anders

Reputation: 423

Declaring empty functions in typescript

I came over a piece of code in a custom component that I have some trouble understanding.

public onTouch: () => void = () => {}

The onTouch function is set by the private method

registerOnTouch(fn: any) {
  this.onTouch = fn;
}

I think that the first part public OnTouch: () = void declares a function without parameters that does not return a value. But I struggle to understand the last part () => ().

What does the latter part describe Could it be an overload?

Upvotes: 0

Views: 229

Answers (1)

kidney
kidney

Reputation: 3093

The first part:

() => void

declares the type of function onTouch and the second part:

= () => {}

declares its default value. That is, in case onTouch is not assigned another value, it will be a no-op.

Upvotes: 2

Related Questions