sentinel-error
A function declared string that returns '' for "no error" and a message otherwise, with callers that test the string: if (error), if (!error), error ? a : b, error === ''. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: nothing makes a caller test the string, and '' reads as a value, not as "it worked". A caller that forgets the test shows an empty message, or carries on after a failure. Say what the function returns instead:
- a command that can fail (it buys, sells, saves) returns
Result<void, E>:err(...)when it fails,undefinedwhen it works. Callers have to handle it (must-use-result), andEcan say more than a message. - a reason that may be missing (why a button is off) returns
Option<string>:undefinedwhen there is none. When callers show it in more than one way, anOptionof anEnumof the reasons says more.
The message picks one: a function that changes state (a Mut<...> parameter, an assignment to something it didn't create, push/set on it) gets Result<void, E>, one that only reads gets Option<string>. The help names the other, for when the guess is wrong. Guide: Errors.
❌ Rejected: a command that fails with a message
✕ Rejectedsentinel-errorstrict-truthiness
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Wallet = { balance: number };
export function buy(wallet: Mut<Wallet>, price: number): string {
if (price > wallet.balance) return `costs ${price} EUR`;
wallet.balance -= price;
return "";
}
export function buyAndReport(wallet: Mut<Wallet>, price: number, show: (text: string) => void): void {
const error = buy(mut(wallet), price);
if (error) show(error);
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Wallet = { balance: number };
export function buy(wallet: Mut<Wallet>, price: number): string {
if (price > wallet.balance) return `costs ${price} EUR`;
wallet.balance -= price;
return "";
}
export function buyAndReport(wallet: Mut<Wallet>, price: number, show: (text: string) => void): void {
const error = buy(wallet, price);
if (error) show(error);
}✅ Accepted: the failure is a Result
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
type Wallet = { balance: number };
export type BuyError = Enum<{ TooExpensive: { missing: number } }>;
export const BuyError = constructors<BuyError>();
export function buy(wallet: Mut<Wallet>, price: number): Result<void, BuyError> {
if (price > wallet.balance) return err(BuyError.TooExpensive({ missing: price - wallet.balance }));
wallet.balance -= price;
return undefined;
}
export function buyAndReport(wallet: Mut<Wallet>, price: number, show: (text: string) => void): void {
const bought = buy(mut(wallet), price);
if (!isErr(bought)) return;
show(match(bought.error, { TooExpensive: ({ missing }) => `You need ${missing} EUR more` }));
}ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
type Wallet = { balance: number };
export type BuyError = Enum<{ TooExpensive: { missing: number } }>;
export const BuyError = constructors<BuyError>();
export function buy(wallet: Mut<Wallet>, price: number): Result<void, BuyError> {
if (price > wallet.balance) return err(({ tag: "TooExpensive", missing: price - wallet.balance } satisfies BuyError as BuyError));
wallet.balance -= price;
return undefined;
}
export function buyAndReport(wallet: Mut<Wallet>, price: number, show: (text: string) => void): void {
const bought = buy(wallet, price);
if (!isErr(bought)) return;
show(match(bought.error, { TooExpensive: ({ missing }) => `You need ${missing} EUR more` }));
}❌ Rejected: a reason that may be missing
✕ Rejectedsentinel-errorstrict-truthiness
ts
type Wallet = { readonly balance: number };
export function buyBlock(wallet: Wallet, price: number): string {
return price > wallet.balance ? `costs ${price} EUR` : "";
}
export function buyTitle(wallet: Wallet, price: number): string {
const block = buyBlock(wallet, price);
return block ? `Can't buy: ${block}` : "Buy";
}✅ Accepted: no reason is undefined, and a reason is a named kind the title is made from
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Wallet = { readonly balance: number };
export type BuyBlock = Enum<{ TooExpensive: { missing: number } }>;
export const BuyBlock = constructors<BuyBlock>();
export function buyBlock(wallet: Wallet, price: number): Option<BuyBlock> {
return price > wallet.balance ? BuyBlock.TooExpensive({ missing: price - wallet.balance }) : undefined;
}
export function buyTitle(wallet: Wallet, price: number): string {
return match(buyBlock(wallet, price), {
TooExpensive: ({ missing }) => `Can't buy: ${missing} EUR short`,
undefined: () => "Buy",
});
}ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Wallet = { readonly balance: number };
export type BuyBlock = Enum<{ TooExpensive: { missing: number } }>;
export const BuyBlock = constructors<BuyBlock>();
export function buyBlock(wallet: Wallet, price: number): Option<BuyBlock> {
return price > wallet.balance ? ({ tag: "TooExpensive", missing: price - wallet.balance } satisfies BuyBlock as BuyBlock) : undefined;
}
export function buyTitle(wallet: Wallet, price: number): string {
{ const __ms1 = buyBlock(wallet, price); let __mv1: string; switch (__ms1?.tag) {
case "TooExpensive": { const { missing } = __ms1; __mv1 = `Can't buy: ${missing} EUR short`; break; }
case undefined satisfies typeof __ms1: { __mv1 = "Buy"; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }
}✅ Also accepted: an Option<string>, what the message suggests, where the reason is only ever shown as it is
✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";
type Wallet = { readonly balance: number };
export function buyBlock(wallet: Wallet, price: number): Option<string> {
return price > wallet.balance ? `costs ${price} EUR` : undefined;
}
export function buyTitle(wallet: Wallet, price: number): string {
const block = buyBlock(wallet, price);
return block === undefined ? "Buy" : `Can't buy: ${block}`;
}What makes it a finding. The function is the project's own, declared string (or returns only string literals), has a return '' (or cond ? message : '') and a return of something else, and a call somewhere in the same package tests the result, directly or through a const. A function that tests another one's result and returns it (if (error) return error) passes the '' on, and is reported too.
A formatter that returns '' for no input isn't reported: its callers use the text, and when they test it, it's to format it (label !== '' ? ` (${label})` : ''), where '' stays "nothing to show":
✅ Accepted: '' is an empty label
✓ Accepted0 diagnostics
ts
export function priceLabel(price: number): string {
return price > 0 ? `${price} EUR` : "";
}
export function title(name: string, price: number): string {
const label = priceLabel(price);
return `${name}${label !== "" ? ` (${label})` : ""}`;
}No quick fix: the return type changes, and every caller with it.