5.12.03.2.2class Some {
private _definitions: number[] = [];
public updateDefinition(def: number): number {
return def * 2;
}
public updateDefinitions(): void {
this._definitions = this._definitions.map(this.updateDefinition, this);
}
}
with tslint.json configuration:
{
"rules": {
"no-unbound-method": true
}
}
Error for this._definitions = this._definitions.map(this.updateDefinition, this);:
ERROR: xx:xx no-unbound-method Avoid referencing unbound methods which may cause unintentional scoping of 'this'.
Has no errors, because I passed the context using the second argument of the .map method. Also, the call occurs within the class.
fixed in PR #4440
+1 for the issue as described, but let's pause and discuss a bit before jumping to a solution. #4440 only fixes it for Array methods but doesn't fix it for similar code written by users.
The rule is also falsely complaining on the following:
function forEveryOther<T>(array: T[], method: (param: T) => void, scope?: unknown) {
for (let i = 0; i < array.length; i += 2) {
method.call(scope, array[i]);
}
}
const textLogger = new TextLogger();
// Still throws an error :(
forEveryOther(["foo", "bar"], textLogger.log, textLogger);
Let's discuss to see if there's a good way the rule can avoid non-Array cases like this one.
TSLint is being deprecated (#4534) and this issue hasn't had discussion posted since the request for clarification in January. Closing this issue for housekeeping. 馃槩
Feel free to comment here if you'd like to propose a technical solution that would allow the rule to be improved!
Most helpful comment
fixed in PR #4440