TypeScript Reference ยท tsconfig: Type Checking Options
TypeScript strictBindCallApply option
Reference for strictBindCallApply in TypeScript: how bind, call and apply are checked against the real function signature, with errors and fixes.
Every JavaScript function has the methods call, apply and bind. In older TypeScript they were typed loosely, accepting any arguments and returning any. With strictBindCallApply they are checked against the function's real signature: the argument types are verified, the right number of arguments is required, and bind returns a correctly typed function with the bound arguments removed.
The flag is part of strict. It works through the built-in CallableFunction and NewableFunction interfaces, which declare generic versions of these methods.
function add(a: number, b: number): number { return a + b; } add.call(null, 1, 2); // fine, returns number // add.call(null, "1", 2); // error under the flag
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
{ "compilerOptions": { "strictBindCallApply": true } }
fn.call(thisArg, ...args) // args checked against fn's parameters fn.apply(thisArg, argsTuple) // argsTuple must match the parameter tuple fn.bind(thisArg, ...first) // returns a function taking the remaining parameters
| Method | Returns | What is checked |
|---|---|---|
call |
The function's return type | thisArg against the declared this, each argument against its parameter. |
apply |
The function's return type | A tuple of arguments matches the parameter list. |
bind |
A function of the remaining parameters | thisArg and the leading arguments; up to a fixed number of bound arguments are typed precisely (beyond that, rest typing applies). |
Edge cases: overloaded functions are resolved using only the last overload for call, apply and bind, which can give surprising errors. Generic functions lose their generics when bound (the type parameters become their constraints). Optional parameters stay optional.
Usage notes
- Without the flag these methods take
...args: any[]and returnany, so wrong calls pass silently. - Arrow functions ignore
thisArg;bindon an arrow function still type-checks the arguments but cannot changethis. - Prefer arrow functions or closures to
bindin modern code; they infer more naturally and need nothishandling. applywith a spread array of unknown length (number[]) fails against fixed parameters. Use a tuple type or spread directly:fn(...args).- Declare
thisin function parameters (this: T) socallandbindcan verify the receiver; seethisin functions. - Methods taken from classes lose
thiswhen passed around;bind(instance)or an arrow-function field is the typical fix. - Overload surprise: if
bindpicks the wrong overload, wrap the call in a small typed function.
Related
- strict
- strictFunctionTypes
- Tutorial: this in functions, Functions