The right side of in or instanceof is a value the operator cannot test against

tx0/ts:membership-test-misusedefaultwarningbug

What it finds

"a" in "abc", key in ["a", "b"], x instanceof "Date", x instanceof Props: the right side of in and instanceof does not fit the operator.

- in with a string, number or other primitive literal on the right throws a TypeError; with an array literal, it tests the indexes "0", "1", ..., not the values, so "a" in ["a"] is false. includes tests the values. - instanceof with a literal, an arrow function or another value that is not a constructor on the right throws a TypeError; so does one whose right side is a TypeScript interface or type alias, which do not exist at run time.

From the rule's source, membership_test_misuse.rs.

Example from the rule's tests
interface Props {  id: number;}function test(key: string, x: unknown) {  const a = key in "abc";tx0 reports: a primitive, and throws  const b = key in ["a", "b"];tx0 reports: in tests the indexes of an array, not its values  const c = x instanceof "Date";tx0 reports: is not a constructor, so instanceof throws  const d = x instanceof Props;tx0 reports: Props is only a type and does not exist at run time  const e = x instanceof Date && key in { a: 1 };  return [a, b, c, d, e];}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:membership-test-misuse" = "off"

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:membership-test-misuse] reason="..."

tx0 explain tx0/ts:membership-test-misuse shows the same in the terminal. Found a false positive? Report it.