Dev Helpers
← Back

Pass a callback returning void

Pass a callback returning void using TypeScript.

Open this example in the interactive library

Source code

type Task = (name: string, attempt?: number) => string;
const task: Task = (name, attempt = 1) => name + " #" + attempt;
console.log(
  (() => {
    const rows: string[] = [];
    const record: (v: string) => void = (v) => {
      rows.push(v);
    };
    record("ready");
    return rows;
  })(),
);

Sample output

[ 'ready' ]