Codectionary / Developer documentation / TypeScript

Checking bind, call and apply

Functions expose call, apply and bind to invoke them with a chosen receiver or prefilled arguments.

What it is and why to use it

Functions expose call, apply and bind to invoke them with a chosen receiver or prefilled arguments. strictBindCallApply checks those argument lists against the function’s signature. It is included in strict and helps catch mistakes that would otherwise reach a dynamic invocation.

Common mistakes and limitations

This option does not repair a wrong this binding or validate unknown arrays. Overloaded and generic functions can have inference limitations through these helpers. Prefer a small explicit wrapper when that makes the intended call easier to understand.

Run this example

Use TypeScript 5.8 or later with strict checking and an ES2022 target. Save each complete example independently as example.ts, compile it, then run the generated JavaScript with a compatible Node.js installation. Compiler-only options below affect checking; they do not add runtime validation.

Terminal — compile and run

npx tsc example.ts --strict --target ES2022 --module commonjs
node example.js

Syntax

"strictBindCallApply": true

Examples

Checking bind, call and apply

Standalone TypeScript 5.8+ example; expected output is explained below.

function add(left: number, right: number): number { return left + right; }
const plusTwo = add.bind(undefined, 2);
console.log(plusTwo(3));
console.log(add.call(undefined, 4, 5));
const pair: [number, number] = [6, 7];
console.log(add.apply(undefined, pair));
  1. bind creates a new function with left fixed at 2.
  2. call supplies arguments individually, while apply receives the typed argument tuple.
  3. undefined is the receiver here because add does not use this. TypeScript checks the numeric inputs but JavaScript still performs the calls at runtime.
  4. Expected output: 5 9 13

Best practices

  • This option does not repair a wrong this binding or validate unknown arrays. Overloaded and generic functions can have inference limitations through these helpers. Prefer a small explicit wrapper when that makes the intended call easier to understand.
  • Keep external-input validation separate from compile-time type descriptions.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading