ProgrammingFullstack Developer

How does the strictBindCallApply parameter work in TypeScript and what does it change in the type safety of the bind/call/apply methods? Describe all the nuances and real risks.

Pass interviews with Hintsage AI assistant

Answer.

History of the issue:

JavaScript allows the use of the bind, call, and apply methods to pass context (this) and arguments. Before TypeScript 3.2, these methods had a looser interpretation: their arguments often accepted extra or incorrect values, leading to runtime errors. TypeScript implemented the strictBindCallApply parameter, which adds strict checking to the signatures of these methods.

The problem:

Without strict typing, the bind/call/apply methods allowed "sneaking through" extra, missing, or type-mismatched arguments without any compile-time errors.

The solution:

With strictBindCallApply enabled, the TypeScript compiler strictly requires that:

  • arguments passed to these methods fully match the declared signature of the method
  • extra, missing, or type-mismatched parameters cannot be passed
  • the this parameter must be consistent with the context type.

Code example:

function sum(a: number, b: number): number { return a + b; } const sum2 = sum.bind(null, 1); sum2(2); // OK sum2(2, 3); // Error in strictBindCallApply

Key features:

  • Eliminates passing extra or incorrect arguments via bind/call/apply at compile time;
  • Improves support for this and function parameters when partially applying;
  • Ensures that the result of bind/call/apply is correctly typed.

Trick questions.

Does strictBindCallApply only work for functions, or does it also affect class methods?

It affects both functions and methods since the bind/call/apply methods are available on any function (Function.prototype). This applies to both standalone functions and class methods.

Can the strictBindCallApply parameter be enabled without strict mode?

No, it requires strict=true or direct specification of strictBindCallApply=true in tsconfig.json.

What happens if more parameters are passed via bind than defined in the original function?

The TypeScript compiler with strictBindCallApply enabled will issue an error: "Expected X arguments, but got Y". This protects against the common error of prolonged signatures or accidentally capturing extra variables.

Typical errors and anti-patterns

  • Using bind with extra parameters to simulate function overloading;
  • Violating the order of arguments with call/apply due to undefined parameter types;
  • Implicitly ignoring the type of this (especially when using arrow functions).

Real-life example

** Negative case A developer uses .bind for partially applying arguments but makes a mistake in their count. In regular mode, there are no errors, but in production, the function is called with an extra parameter, and the logic breaks.

Pros:

  • More flexible function implementation through bind/apply, no need to worry about the signature.

Cons:

  • Traps are hard to debug, potential bugs in production.

** Positive case With strictBindCallApply enabled, functions and methods use bind strictly according to the signature: extra or incorrect arguments are filtered out at compile time.

Pros:

  • Errors are visible during build, before the code reaches production;
  • Increases code reliability, debugging is significantly easier.

Cons:

  • Gradual "fixing" of old code, sometimes requiring refactoring parts of the project.