this, void and Callbacks
Functions can declare the type of this. void means the return value is ignored, which is why callbacks returning values are still accepted.
Example
const counter = {
n: 0,
inc(this: { n: number }) { this.n++; return this.n; },
};
console.log(counter.inc());
const out: number[] = [];
[1, 2, 3].forEach((x): void => { out.push(x * 2); });
console.log(out);Result
1 [ 2, 4, 6 ]
Exercise
Return nothing.
function f(): {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.