Rules Hub

Coding Rules Library

← Back to all rules
fullstack ruleStack: typescript
typescripttype-safetyclean-codeinference

Prefer instanceof over Boolean for type narrowing

Use instanceof checks in filter callbacks to allow TypeScript to automatically infer types and avoid manual type predicates.

PR: Feat/FCK-2245 - Cache Bellsheep and profile loaders with TanStack Query #343Created: Dec 10, 2025

Bad example

Old codets
1const signals: (AbortSignal | null | undefined)[] = [/* ... */];
2
3// Requires explicit type predicate to tell TS what happened
4const activeSignals = signals.filter((signal): signal is AbortSignal => {
5 return Boolean(signal);
6});

Explanation (EN)

Using `Boolean(signal)` relies on generic truthiness and requires a manual type predicate (`signal is AbortSignal`) to ensure the array type is narrowed correctly. It incorrectly suggests that any truthy value is valid.

Objašnjenje (HR)

Korištenje `Boolean(signal)` oslanja se na generičku istinitost i zahtijeva ručni predikat tipa (`signal is AbortSignal`) kako bi se osiguralo ispravno sužavanje tipa niza. Pogrešno sugerira da je bilo koja istinita vrijednost valjana.

Good example

New codets
1const signals: (AbortSignal | null | undefined)[] = [/* ... */];
2
3// TS automatically infers 'AbortSignal[]' (in TS 5.5+)
4const activeSignals = signals.filter((signal) => {
5 return signal instanceof AbortSignal;
6});

Explanation (EN)

Using `instanceof` provides a precise runtime check. Modern TypeScript (5.5+) can automatically infer the type predicate from this check, eliminating the need for manual annotations and preventing casting errors.

Objašnjenje (HR)

Korištenje `instanceof` pruža preciznu provjeru u vremenu izvođenja. Moderni TypeScript (5.5+) može automatski zaključiti predikat tipa iz ove provjere, eliminirajući potrebu za ručnim anotacijama i sprječavajući greške pri pretvorbi.

Notes (EN)

Inferred type predicates from filter functions were introduced in TypeScript 5.5.

Bilješke (HR)

Izvedeni predikati tipova iz funkcija filtriranja uvedeni su u TypeScript 5.5.