Dev Helpers
← Back

Bind a function receiver

Bind a function receiver 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(
  function (this: { prefix: string }, v: string) {
    return this.prefix + v;
  }.bind({ prefix: "job:" })("42"),
);

Sample output

job:42