Skip to content

no-type-predicate ​

A hand-written type predicate (value is User, this is Open) or assertion signature (asserts value is User, asserts value) on a function, method or arrow of the project. On by default, as an error.

Why: TypeScript never checks a predicate's body against its claim. function isUser(v: unknown): v is User { return true } compiles, and so does an assertion with an empty body. After the call, TypeScript trusts every field of User, so the predicate is a cast (no-unchecked-cast) with a name. It also lies on the false side: (v): v is number => typeof v === "number" && v > 0 tells TypeScript that -1 is not a number. A predicate that claims a brand (text is SafeUrl) is reported by brand-constructor instead.

What narrows a value without a claim: typeof, instanceof, Array.isArray, "key" in x, a discriminant test (shape.kind === "circle"), std's guards (is(x, Variant), isSome, isErr, isAscii, isJsonObject), and for data of unknown shape validate<T> or parseJson<T>, which are generated from the type.

❌ Rejected: the body checks two fields of three, and nothing says so

✕ Rejectedno-type-predicate
ts
type User = { readonly id: string; readonly name: string; readonly email: string };

export function isUser(value: unknown): value is User {
  return typeof value === "object" && value !== null && "id" in value && "name" in value;
}

✅ Accepted: validate<T> checks every field, and the error says which one is wrong

✓ Accepted0 diagnostics
ts
import { type JsonError, validate } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type User = { readonly id: string; readonly name: string; readonly email: string };

export function storedUser(value: unknown): Result<User, JsonError> {
  return validate<User>(value);
}
ts
import { parseJsonWith as __jsonParse, validateWith as __jsonValidate, jsonFail as __jsonFail, isJsonErr as __jsonIsErr, isJsonObject as __jsonIsObject, type JsonResult as __JsonResult } from "@evobug_com/safe-ts-std/json"; import { type JsonError, validate } from "@evobug_com/safe-ts-std/json";
import type { Result } from "@evobug_com/safe-ts-std/result";

type User = { readonly id: string; readonly name: string; readonly email: string };

export function storedUser(value: unknown): Result<User, JsonError> {
  return __jsonValidate<User>(value, __v0);
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "id": string; "name": string; "email": string; };
function __v0(v: unknown, p: string): __JsonResult<__T0> {
  if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
  const __j1 = v["id"];
  if (typeof __j1 !== "string") return __jsonFail(p + ".id", "string", __j1);
  const __j2 = __j1;
  const __j3 = v["name"];
  if (typeof __j3 !== "string") return __jsonFail(p + ".name", "string", __j3);
  const __j4 = __j3;
  const __j5 = v["email"];
  if (typeof __j5 !== "string") return __jsonFail(p + ".email", "string", __j5);
  const __j6 = __j5;
  const out: { "id": string; "name": string; "email": string; } = { "id": __j2, "name": __j4, "email": __j6 };
  return out;
}

A body that is one test: drop the claim ​

Since TypeScript 5.5, a function without a return type whose body is a test TypeScript narrows by gets that predicate inferred, and checked. So a predicate whose body is exactly such a test doesn't need its annotation:

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

type User = { readonly id: string; readonly name: string };

export function present(users: readonly Option<User>[]): User[] {
  return users.filter((user): user is User => user !== undefined);
}

✅ Accepted: the same test, and TypeScript infers user is User from it

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

type User = { readonly id: string; readonly name: string };

export function present(users: readonly Option<User>[]): User[] {
  return users.filter(isSome);
}

export function presentToo(users: readonly Option<User>[]): User[] {
  return users.filter((user) => user !== undefined);
}

The quick fix Let TypeScript infer the narrowing from the body removes : x is T where the body is one of these tests, or several joined by || (or &&, on the value itself): typeof x === "string", x instanceof C (not for a claim with type arguments, x is Map<string, Price>: TypeScript would infer Map<any, any>), Array.isArray(x) (not on a readonly array, which it doesn't narrow), "key" in x, x === <literal>, x !== undefined, !!x, x.kind === "circle" (a literal or a constant like Op.Move), a call of std's guards on x. It changes types only. Where the claim was more than the test ((x): x is User => x !== undefined over unknown[]), the inferred predicate is the test's ({} | null), and the code that relied on the claim stops compiling: that is the claim being checked for the first time.

Instead of a predicate ​

A predicate that...Write
checks data of unknown shape (value: unknown)validate<T>(value), or parseJson<T>(text): a Result
checks a value is one of a list (KINDS.some((k) => k === name))KINDS.find((k) => k === name): an Option<Kind> TypeScript types for you
checks a table's key (Object.hasOwn(TABLE, name))getOwn(TABLE, name) (std dict) for the entry, keysOf(TABLE).find((k) => k === name) for the key
checks Record<string, unknown> (typeof v === "object" && v !== null)std's isJsonObject(v) or isJsonPlainObject(v) (std json)
filters an Option list.filter(isSome)
filters an enum's variant.filter((e) => is(e, "Moved")), also is(e, tag) with a tag in a variable; a second test after it needs no claim: .filter((e) => is(e, "Moved")).filter((e) => e.far)
tests something TypeScript can't narrow by (a range, a tuple's element)a loop that keeps what passes, or a function that returns Option<T>
asserts (asserts value is User)return the Result (or Option) of the check, or write if (!test) panic("...") where the value is used

❌ Rejected: a value checked against a list, claimed

✕ Rejectedno-type-predicate
ts
const UNIT_KINDS = ["worker", "scout", "guard"] as const;
type UnitKind = (typeof UNIT_KINDS)[number];

function isUnitKind(name: string): name is UnitKind {
  return UNIT_KINDS.some((kind) => kind === name);
}

export function spawnCost(name: string): number {
  return isUnitKind(name) ? name.length * 10 : 0;
}

✅ Accepted: the value found, typed by TypeScript

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

const UNIT_KINDS = ["worker", "scout", "guard"] as const;
type UnitKind = (typeof UNIT_KINDS)[number];

export function unitKind(name: string): Option<UnitKind> {
  return UNIT_KINDS.find((kind) => kind === name);
}

export function spawnCost(name: string): number {
  const kind = unitKind(name);
  return kind === undefined ? 0 : kind.length * 10;
}

The one assertion allowed ​

asserts value (no type) whose body is exactly if (!value) throw ... or if (!value) panic(...): it claims nothing about a type, and the body establishes all it says. TypeScript doesn't infer assertions, so there is nothing to drop the annotation for.

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

export function check(ok: boolean, message: string): asserts ok {
  if (!ok) panic(message);
}

export function half(n: number): number {
  check(n % 2 === 0, `odd: ${n}`);
  return n / 2;
}

Where it isn't reported ​

  • Signatures without a body: overloads (see no-overload), declare function, an interface or type that says (value: unknown) => value is User. A function given that type must itself be a predicate, and is reported where it is written.
  • .d.ts files, declare module, declare global, declare namespace, declare class: they describe code from elsewhere.
  • Files marked // @safe-ts std: std's own guards live there. A check that can't be written (a method's signature on an object a library made) can too, reviewed and counted, or one trust<T>(value) (std result) at the use site.

What the rule can't see ​

  • A predicate written in a declare function in a .ts file whose implementation is plain JavaScript elsewhere.
  • Whether a trust<T>(value) or a // @safe-ts std module tells the truth: they are counted in safe-ts check's escape-hatch line, not checked.

Related: no-overload, the other signature TypeScript takes on trust; no-unchecked-cast. It replaced partial-type-guard, which reported a predicate over unknown only when it skipped a field.

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