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""or0, as the test ofif,while,do,forand?:, an operand of&&and||in such a test, the argument of!, the left side of&&anywhere, the condition ofv-if,v-else-ifandv-show, and the items offilter,find,some,every(and the otherfind…methods) givenBooleanor a callback that returns a value. - A value that may be
undefinedis 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. Writenickname !== undefinedwhen an empty nickname is a nickname, ornickname !== undefined && nickname !== ""when it isn't: then the reader sees that it was decided. Nevernickname !== ""alone: it is true forundefined(compare-misses-undefinedreports that). A value the code has already proven defined there is tested as thenumberorstringit is, and the message and fixes say so:b.sizeinsideif (b.size !== undefined),list.lengthwhereconst list = b.items.filter(f)followsif (!b.items?.some(g)) return;. What is followed, and what undoes it (a write, a call that gets the object, anawait), is the same as forcompare-misses-undefined(what is followed). - A bit test is a number too:
if (flags & 4)is reported, as Rust'sif flags & 4 != 0asks: 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;letwidens it to astring, which is. [a && "x", b && "y"].filter(Boolean)is reported once, at thefilter, 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,Optionof any of those (if (user)), anInt<Min, Max>that can't be 0 (Int<1, 12>), the items of anas constarray (for (const [name, on] of CASES) if (on)tests aboolean), and a field picked by a key union (rules[field]withfield: "name" | "age"is one of those fields).x || fallbackas a value is a default, not a test (prefer-nullish-defaultcoversn || 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-typeasks for the missing types.
Quick fixes
Where the type is known, in this file or another one, the fixes write the comparison:
| Type | Fixes (for x and !!x; for !x the opposite) |
|---|---|
number | Compare with 0 (NaN counts as true) (x !== 0), Test for a positive number (x > 0; !(x > 0) for !x) |
string | Compare with "" (x !== "") |
bigint | Compare 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 undefined | Test 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.