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:
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:
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.
** 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:
Cons:
** 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:
Cons: