Skip to main content

guard

Compile-time predicates returning true | false — IsString, IsObject, IsRecord, IsMap, ...

import type {
  IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol,
  IsObject, IsArray, IsReadonlyArray, IsEmpty,
  IsTrue, IsFalse,
  IsDate, IsRegExp, IsError,
  IsMap, IsSet, IsWeakMap, IsWeakSet,
  IsPromiseLike,
  IsRecord, IsIterable, IsAsyncIterable,
  IsArrayBuffer, IsDataView, IsTypedArray,
  IsFixedTuple,
} from '@gentleduck/ttest/guard'

Compose with If / IfExtends from conditional to branch on the result.

Primitive guards

type IsString <T>  = [T] extends [string]  ? true : false
type IsNumber <T>  = [T] extends [number]  ? true : false
type IsBoolean<T>  = [T] extends [boolean] ? true : false
type IsBigInt <T>  = [T] extends [bigint]  ? true : false
type IsSymbol <T>  = [T] extends [symbol]  ? true : false

Each accepts the wider primitive or any literal subtype.

type a = IsString<'hello'>  // true
type b = IsNumber<42>       // true
type c = IsString<number>   // false

IsObject

type IsObject<T>

true if T is a plain object — not an array, function, or primitive.

IsArray / IsReadonlyArray

type IsArray<T>          // mutable or readonly
type IsReadonlyArray<T>  // only `readonly X[]`, not `X[]`

IsEmpty

type IsEmpty<T>

true for empty strings, arrays, and objects.

type a = IsEmpty<''>           // true
type b = IsEmpty<[]>           // true
type c = IsEmpty<{}>           // true
type d = IsEmpty<{ a: 1 }>     // false

IsTrue / IsFalse

type IsTrue<T>   // exactly the literal true
type IsFalse<T>  // exactly the literal false

Both reject boolean itself — they want the literal.

Built-in object guards

type IsDate<T>
type IsRegExp<T>
type IsError<T>
type IsMap<T>            // Map of some shape
type IsSet<T>
type IsWeakMap<T>
type IsWeakSet<T>
type IsPromiseLike<T>
type a = IsDate<Date>         // true
type b = IsMap<Map<string, number>>  // true

IsRecord

type IsRecord<T>

Plain Record<PropertyKey, unknown> — excludes arrays, functions, Date, RegExp, Error, Map, Set, WeakMap, WeakSet.

IsIterable / IsAsyncIterable

type IsIterable<T>       // sync iterator protocol
type IsAsyncIterable<T>  // async iterator protocol

Typed-array guards

type IsArrayBuffer<T>
type IsDataView<T>
type IsTypedArray<T>  // Uint8Array | Int16Array | Float32Array | ...

IsFixedTuple

type IsFixedTuple<T>

true if T is a tuple with a literal length (not a T[] whose length is number).

type a = IsFixedTuple<[1, 2, 3]>  // true
type b = IsFixedTuple<number[]>   // false