Skip to content

strict-truthiness ​

A test of a value whose type includes number, string or bigint: if (count), name && ..., !id, x ? a : b, while (n), v-if="item.count", v-show="title", .filter(Boolean) and .filter((x) => x.label). An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: each of those types has a value that counts as false: 0 (and NaN), "", 0n. The test treats it like a missing value, which is rarely what it means: v-if="item.count" hides the item with no stock, if (name) skips the user whose name is empty, count && <b>… renders a 0 in React. Saying what is tested makes the case visible and costs nothing: count > 0, name !== undefined, name !== "". Guide: Proof types.

✕ Rejectedstrict-truthiness
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

type Item = { readonly label: string; readonly stock: number; readonly note: Option<string> };

export function badge(item: Item): string {
  if (!item.stock) return "sold out";
  return item.note ? `${item.label}: ${item.note}` : item.label;
}

✅ Accepted: the comparison that is meant

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

type Item = { readonly label: string; readonly stock: number; readonly note: Option<string> };

export function badge(item: Item): string {
  if (item.stock <= 0) return "sold out";
  return item.note !== undefined ? `${item.label}: ${item.note}` : item.label;
}

The same in a template:

✕ Rejectedstrict-truthiness
vue
<script setup lang="ts">
type Item = { readonly label: string; readonly stock: number };

const props = defineProps<{ items: readonly Item[]; title?: string }>();
</script>

<template>
  <h2 v-if="props.title">{{ props.title }}</h2>
  <ul>
    <li v-for="item in props.items" :key="item.label" v-show="item.stock">{{ item.label }}</li>
  </ul>
</template>
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
type Item = { readonly label: string; readonly stock: number };

const props = defineProps<{ items: readonly Item[]; title?: string }>();
</script>

<template>
  <h2 v-if="props.title !== undefined">{{ props.title }}</h2>
  <ul>
    <li v-for="item in props.items" :key="item.label" v-show="item.stock > 0">{{ item.label }}</li>
  </ul>
</template>

What counts ​

  • Reported: a value whose type includes number, string, bigint, a brand (a string), or the literal "" or 0, as the test of if, while, do, for and ?:, an operand of && and || in such a test, the argument of !, the left side of && anywhere, the condition of v-if, v-else-if and v-show, and the items of filter, find, some, every (and the other find… methods) given Boolean or a callback that returns a value.
  • A value that may be undefined is reported too, and the message says so: an optional parameter or field (id?: string), T | undefined, Option<string>, a?.b, map.get(k), list.find(...), and those types declared in other files. if (nickname) treats "" like a missing nickname, which is the bug this rule is about. Write nickname !== undefined when an empty nickname is a nickname, or nickname !== undefined && nickname !== "" when it isn't: then the reader sees that it was decided. Never nickname !== "" alone: it is true for undefined (compare-misses-undefined reports that). A value the code has already proven defined there is tested as the number or string it is, and the message and fixes say so: b.size inside if (b.size !== undefined), list.length where const list = b.items.filter(f) follows if (!b.items?.some(g)) return;. What is followed, and what undoes it (a write, a call that gets the object, an await), is the same as for compare-misses-undefined (what is followed).
  • A bit test is a number too: if (flags & 4) is reported, as Rust's if flags & 4 != 0 asks: write (flags & 4) !== 0, the rewrite the quick fix makes. A literal keeps its type where TypeScript keeps it: const icon = ok ? "icon-a" : undefined; if (icon) tests "icon-a" | undefined, which can't be "", and isn't reported; let widens it to a string, which is.
  • [a && "x", b && "y"].filter(Boolean) is reported once, at the filter, and only when an item can be falsy for a reason other than its condition: "x" and "y" can't be, so with boolean conditions nothing is reported.
  • Left alone: boolean, objects, arrays, functions, Option of any of those (if (user)), an Int<Min, Max> that can't be 0 (Int<1, 12>), the items of an as const array (for (const [name, on] of CASES) if (on) tests a boolean), and a field picked by a key union (rules[field] with field: "name" | "age" is one of those fields). x || fallback as a value is a default, not a test (prefer-nullish-default covers n || 10). Written constants (while (true)).
  • Where the type isn't known, nothing is reported. The type comes from the code: a declared type (a parameter, a field, a prop, also in other files, through type aliases), a literal, what a function is declared to return, or, without a declared return type, what it plainly returns (what is read off). A value the code doesn't type (a result that isn't plain, a package's value, unknown) is left alone: the rule doesn't guess. undeclared-return-type asks for the missing types.

Quick fixes ​

Where the type is known, in this file or another one, the fixes write the comparison:

TypeFixes (for x and !!x; for !x the opposite)
numberCompare with 0 (NaN counts as true) (x !== 0), Test for a positive number (x > 0; !(x > 0) for !x)
stringCompare with "" (x !== "")
bigintCompare with 0n (x !== 0n)
may be undefined: x?: string, string | undefined, Option<string>, a?.b, map.get(k)Test for undefined and "" (the same as now) (x !== undefined && x !== ""), Test for undefined only (x !== undefined)
a number that may be undefinedTest for undefined and 0 (NaN counts as true) (x !== undefined && x !== 0), Test for undefined only (0 and NaN count as true)
[a && "x", b && "y"].filter(Boolean)Spread the items that are kept ([...(a ? ["x"] : []), ...(b ? ["y"] : [])])

A value that may be undefined never gets a lone x !== "", and its help names both halves. A value read through a call is read once: (items.shift() ?? "") !== "", not two calls that could give two different items. For a number, the comparisons with 0 change one thing: NaN now counts as true, and their titles say so. The spread is offered where each item is a value that can't be falsy ("x", an object); a condition whose type is known is written out (name !== ""), so the meaning stays the same (with (NaN counts as true) in the title when a number is written out). What quick fixes keep lists every fix and what it does to the result. "" is written in the file's quotes: '' where its imports (or, without imports, its strings) use single quotes, and in a Vue template.

safe-ts fix strict-truthiness <dirs> applies Test for undefined and "" (the same as now) by itself, the fix that keeps the result; the fixes for a number each change something (NaN, negative numbers), so those sites are listed with them, and --fix "Compare with 0" (or --fix "Test for a positive") picks one (Applying one rule's quick fixes).

Why it is opt-in: most code tests strings and counts for truth on purpose somewhere, so in an existing codebase the rule reports many places that aren't bugs, and each one needs a decision, not a mechanical fix: !== undefined and !== "" mean different things.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra