Skip to content

Type guards

These guards check a value at runtime and narrow its TypeScript type. They test for a protocol (the method or symbol that makes something a promise, an iterable or a disposable), not for a specific class. Use them in functions that accept “anything iterable” or “a value or a promise” and need to choose a code path.

import {
isPromise,
isIterable,
isAsyncIterable,
isIterator,
isGenerator,
isAsyncGenerator,
isDisposable,
isAsyncDisposable,
isSyncDisposable,
} from "@xtandard/lib";
Guard Narrows to Checks for
isPromise(value) Promise<T> a Promise instance, or any object with a then method
isIterable(value) Iterable<T> an object with a [Symbol.iterator] method
isAsyncIterable(value) AsyncIterable<T> an object with a [Symbol.asyncIterator] method
isIterator(value) Iterator<T> an object with a next method
isGenerator(value) Generator<T, TReturn> next, return and throw methods, and not async-iterable
isAsyncGenerator(value) AsyncGenerator<T, TReturn> [Symbol.asyncIterator], next and return
isSyncDisposable(value) Disposable a [Symbol.dispose] method
isAsyncDisposable(value) AsyncDisposable a [Symbol.asyncDispose] method
isDisposable(value) Disposable | AsyncDisposable either of the above

isPromise returns true for native promises and for any other “thenable” (an object with a then function). It is the check that sync/async-adaptive code uses to decide which path to take.

import { isPromise } from "@xtandard/lib";
function format(value: number | Promise<number>) {
if (isPromise(value)) {
return value.then((n) => n.toFixed(2)); // value: Promise<number>
}
return value.toFixed(2); // value: number
}

The guard’s type is value is PromiseLike<T>, so it narrows to the promise-like part of the input: a number | Promise<number> becomes Promise<number>, and a MaybePromise<T> (T | PromiseLike<T>) becomes PromiseLike<T>. The false branch narrows to T in both cases, also when T is a type parameter, so no value as T cast is needed:

import { isPromise, type MaybePromise } from "@xtandard/lib";
function double<T extends number>(value: MaybePromise<T>) {
if (isPromise(value)) {
return value.then((n) => n * 2); // value: PromiseLike<T>
}
return value * 2; // value: T
}

A true result only promises a then method. On a PromiseLike, .catch and .finally aren’t in the type, and a non-native thenable may not have them at runtime either; await it or call .then.

function toArray<T>(value: T | Iterable<T>): T[] {
return isIterable<T>(value) ? [...value] : [value];
}
toArray([1, 2]); // [1, 2]
toArray(new Set([1, 2])); // [1, 2]
toArray(1); // [1]

The type parameter only sets the element type of the narrowed value; it isn’t checked at runtime.

isAsyncIterable matches async generators, AsyncChannel and any object with Symbol.asyncIterator. A sync iterable of promises does not match.

isIterator only checks for a next method. Generators, array iterators ([][Symbol.iterator]()) and hand-written { next() {...} } objects all pass. Async iterators pass too, because they have a next method as well. To tell sync from async, check isAsyncIterable first, as toAsyncIterable does.

These guards tell a generator object apart from a plain iterator. isGenerator requires next, return and throw, and rejects anything async-iterable. isAsyncGenerator requires Symbol.asyncIterator, next and return.

function* gen() {
yield 1;
}
async function* agen() {
yield 1;
}
isGenerator(gen()); // true
isGenerator(agen()); // false
isGenerator([1, 2][Symbol.iterator]()); // false: no return/throw
isAsyncGenerator(agen()); // true
isAsyncGenerator(gen()); // false

You can pass the yield and return types: isGenerator<number, string>(value) narrows to Generator<number, string, unknown>.

Use these guards when you need the generator-only methods, for example gen.return() to stop a generator early and run its finally blocks.

isDisposable, isAsyncDisposable, isSyncDisposable

Section titled “isDisposable, isAsyncDisposable, isSyncDisposable”

These guards check for the explicit resource management protocols: Symbol.dispose (used by using) and Symbol.asyncDispose (used by await using).

async function release(resource: unknown) {
if (isAsyncDisposable(resource)) await resource[Symbol.asyncDispose]();
else if (isSyncDisposable(resource)) resource[Symbol.dispose]();
}

isDisposable accepts either kind. The two specific guards are separate, and xtandard’s own helpers only implement one protocol each: defer and makeDisposable are async-disposable, and deferSync is sync-disposable.

import { defer, deferSync } from "@xtandard/lib";
isAsyncDisposable(defer(() => {})); // true
isSyncDisposable(defer(() => {})); // false
isSyncDisposable(deferSync(() => {})); // true
isDisposable(deferSync(() => {})); // true

makeDisposable uses isAsyncDisposable to refuse objects that already have a dispose method.