typescript void function code example
Example 1: typescript pass a function as an argunetn
class Foo {
save(callback: (n: number) => any) : void {
callback(42);
}
}
var foo = new Foo();
var strCallback = (result: string) : void => {
alert(result);
}
var numCallback = (result: number) : void => {
alert(result.toString());
}
foo.save(strCallback); // not OK
foo.save(numCallback); // OK
Example 2: void in ts
// Similar to languages like Java, void is used where there is no data.
// For example, if a function does not return any value then you can
// specify void as return type.
function sayHi(): void {
console.log('Hi!')
}
let speech: void = sayHi();
console.log(speech); //Output: undefined