A function expression given to an array method uses this, which is undefined there

tx0/ts:callback-thisrecommendedwarningbug

What it finds

items.map(function (item) { return this.format(item); }): a function expression has its own this, and an array method calls it with this set to undefined unless a second argument says otherwise. this inside does not refer to the object of the code around it, as it would in an arrow function.

Reported are function expressions that use this outside nested functions, given as the callback of forEach, map, filter, some, every, find, findIndex, findLast, findLastIndex or flatMap without the argument that sets this. Libraries with methods of the same names that set this themselves exist, which is why the rule is recommended rather than on by default.

From the rule's source, callback_this.rs.

Example from the rule's tests
class Report {  prefix = "> ";  lines(items: string[]) {    return items.map(function (item) {      return this.prefix + item;tx0 reports: this in this callback is undefined    });  }  bound(items: string[]) {    return items.map(function (item) {      return this.prefix + item;    }, this);  }  arrow(items: string[]) {    return items.map((item) => this.prefix + item);  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:callback-this" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:callback-this] reason="..."

tx0 explain tx0/ts:callback-this shows the same in the terminal. Found a false positive? Report it.