Skip to main content
Type guards are functions that perform runtime checks and narrow TypeScript types, enabling type-safe code execution.

Primitive Type Guards

isString

Checks if a value is a string.
function isString(value: unknown): value is string
value
unknown
The value to check
Returns: boolean - true if the value is a string

Example

const value: unknown = "hello";

if (isString(value)) {
  // TypeScript knows value is a string here
  console.log(value.toUpperCase()); // OK
}

isNumber

Checks if a value is a number.
function isNumber(value: unknown): value is number
value
unknown
The value to check
Returns: boolean - true if the value is a number

Example

const value: unknown = 42;

if (isNumber(value)) {
  console.log(value.toFixed(2)); // OK
}

isBoolean

Checks if a value is a boolean.
function isBoolean(value: unknown): value is boolean
value
unknown
The value to check
Returns: boolean - true if the value is a boolean

Example

const value: unknown = true;

if (isBoolean(value)) {
  console.log(value ? "yes" : "no"); // OK
}

isSymbol

Checks if a value is a symbol.
function isSymbol(value: unknown): value is symbol
value
unknown
The value to check
Returns: boolean - true if the value is a symbol

Example

const value: unknown = Symbol("id");

if (isSymbol(value)) {
  console.log(value.description); // OK
}

Collection Type Guards

isArray

Checks if a value is an array with optional type parameter for array items.
function isArray<TArray>(value: unknown): value is TArray[]
TArray
type parameter
The type of array items
value
unknown
The value to check
Returns: boolean - true if the value is an array

Example

const value: unknown = [1, 2, 3];

if (isArray<number>(value)) {
  // TypeScript knows value is number[] here
  const sum = value.reduce((a, b) => a + b, 0); // OK
}

isSet

Checks if a value is a Set.
function isSet<TSet extends Set<unknown>>(value: unknown): value is TSet
TSet
Set<unknown>
The Set type
value
unknown
The value to check
Returns: boolean - true if the value is a Set

Example

const value: unknown = new Set([1, 2, 3]);

if (isSet(value)) {
  console.log(value.size); // OK
  value.add(4); // OK
}

isMap

Checks if a value is a Map.
function isMap<TMap extends Map<unknown, unknown>>(value: unknown): value is TMap
TMap
Map<unknown, unknown>
The Map type
value
unknown
The value to check
Returns: boolean - true if the value is a Map

Example

const value: unknown = new Map([["key", "value"]]);

if (isMap(value)) {
  console.log(value.get("key")); // OK
  value.set("newKey", "newValue"); // OK
}

Object Type Guards

isObject

Checks if a value is an object (including arrays, null is excluded).
function isObject<TObject extends object>(value: unknown): value is TObject
TObject
object
The object type
value
unknown
The value to check
Returns: boolean - true if the value is an object

Example

const value: unknown = { name: "Alice" };

if (isObject(value)) {
  // TypeScript knows value is an object
  console.log(Object.keys(value)); // OK
}

isObjectAndNotArray

Checks if a value is an object but not an array.
function isObjectAndNotArray<TObject = UnknownObject>(value: unknown): value is TObject
TObject
object
default:"UnknownObject"
The object type
value
unknown
The value to check
Returns: boolean - true if the value is an object and not an array

Example

const obj: unknown = { name: "Alice" };
const arr: unknown = [1, 2, 3];

isObjectAndNotArray(obj); // true
isObjectAndNotArray(arr); // false

isPlainObject

Checks if a value is a plain object (created by Object literal or Object.create).
function isPlainObject<TPlainObject extends UnknownObjectWithAnyKey = UnknownObject>(
  value: unknown
): value is TPlainObject
TPlainObject
object
default:"UnknownObject"
The plain object type
value
unknown
The value to check
Returns: boolean - true if the value is a plain object

Example

class MyClass {}

isPlainObject({}); // true
isPlainObject({ name: "Alice" }); // true
isPlainObject(new MyClass()); // false
isPlainObject([]); // false
isPlainObject(null); // false

Function Type Guards

isFunction

Checks if a value is a function.
function isFunction<TFunction extends AnyFunction>(value: unknown): value is TFunction
TFunction
AnyFunction
The function type
value
unknown
The value to check
Returns: boolean - true if the value is a function

Example

const value: unknown = () => "hello";

if (isFunction(value)) {
  const result = value(); // OK
}

isAsyncFunction

Checks if a value is an async function.
function isAsyncFunction<TAsyncFunction extends AnyAsyncFunction>(
  value: unknown
): value is TAsyncFunction
TAsyncFunction
AnyAsyncFunction
The async function type
value
unknown
The value to check
Returns: boolean - true if the value is an async function

Example

const value: unknown = async () => "hello";

if (isAsyncFunction(value)) {
  const result = await value(); // OK
}

Browser/Web API Type Guards

isFormData

Checks if a value is FormData.
function isFormData(value: unknown): value is FormData
value
unknown
The value to check
Returns: boolean - true if the value is FormData

Example

const value: unknown = new FormData();

if (isFormData(value)) {
  value.append("key", "value"); // OK
}

isFile

Checks if a value is a File.
function isFile(value: unknown): value is File
value
unknown
The value to check
Returns: boolean - true if the value is a File

Example

const value: unknown = new File(["content"], "file.txt");

if (isFile(value)) {
  console.log(value.name); // OK
  console.log(value.size); // OK
}

isBlob

Checks if a value is a Blob.
function isBlob(value: unknown): value is Blob
value
unknown
The value to check
Returns: boolean - true if the value is a Blob

Example

const value: unknown = new Blob(["data"]);

if (isBlob(value)) {
  console.log(value.size); // OK
  console.log(value.type); // OK
}

Utility Type Guards

isPromise

Checks if a value is a Promise.
function isPromise(value: unknown): value is Promise<unknown>
value
unknown
The value to check
Returns: boolean - true if the value is a Promise

Example

const value: unknown = Promise.resolve("data");

if (isPromise(value)) {
  const result = await value; // OK
}

isIterable

Checks if an object is iterable.
function isIterable<TIterable>(obj: object): obj is Iterable<TIterable>
TIterable
type parameter
The type of iterable items
obj
object
The object to check
Returns: boolean - true if the object is iterable

Example

const value = [1, 2, 3];

if (isIterable<number>(value)) {
  for (const item of value) {
    console.log(item); // OK
  }
}

JSON Type Guards

isJsonString

Checks if a value is a valid JSON string.
function isJsonString(value: unknown): value is string
value
unknown
The value to check
Returns: boolean - true if the value is a valid JSON string

Example

const value: unknown = '{"name":"Alice"}';

if (isJsonString(value)) {
  const parsed = JSON.parse(value); // Safe to parse
}

isJSONSerializable

Checks if a value can be serialized to JSON.
function isJSONSerializable(value: unknown): boolean
value
unknown
The value to check
Returns: boolean - true if the value can be serialized to JSON

Example

isJSONSerializable({ name: "Alice" }); // true
isJSONSerializable("string"); // true
isJSONSerializable(42); // true
isJSONSerializable(undefined); // false
isJSONSerializable(() => {}); // false

isSerializableObject

Checks if an object is serializable (plain object, array, or has toJSON method).
function isSerializableObject(value: unknown): boolean
value
unknown
The value to check
Returns: boolean - true if the object is serializable

Example

isSerializableObject({ name: "Alice" }); // true
isSerializableObject([1, 2, 3]); // true
isSerializableObject(new Date()); // true (has toJSON)
isSerializableObject(new Map()); // false

Build docs developers (and LLMs) love