new String, new Number or new Boolean creates an object instead of a primitive

tx0/ts:boxed-primitiverecommendedwarningbug

What it finds

new Boolean(false), new String("a"), new Number(0): new with these makes an object that wraps the primitive, not the primitive. The object is always true in a condition, === compares it by identity, and typeof gives "object"; new Boolean(false) passes an if. Called without new, they convert their argument to a primitive, which is usually what was meant.

The names must refer to the global String, Number and Boolean. The fix removes new and changes what the code does, so it is unsafe.

From the rule's source, boxed_primitive.rs.

Example from the rule's tests
function flags(raw: string) {  const off = new Boolean(false);tx0 reports: new Boolean creates a wrapper object  const name = new String(raw);tx0 reports: new String creates a wrapper object  const count = Number(raw);  return [off, name, count];}function shadowed(String: any) {  return new String("a");}
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:boxed-primitive" = "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:boxed-primitive] reason="..."

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