Fundamental type checking methods for JavaScript primitive types, special TypeScript types, and common data structures.
Check for basic JavaScript primitive types with built-in protection against any types.
/**
* Check if type is a number (including literal number types)
* Protects against any type
*/
toBeNumber: Scolder<ExpectNumber<Actual>, Options>;
/**
* Check if type is a string (including literal string types)
* Protects against any type
*/
toBeString: Scolder<ExpectString<Actual>, Options>;
/**
* Check if type is a boolean (including literal boolean types)
* Protects against any type
*/
toBeBoolean: Scolder<ExpectBoolean<Actual>, Options>;
/**
* Check if type is bigint (including literal bigint types)
* Protects against any type
*/
toBeBigInt: Scolder<ExpectBigInt<Actual>, Options>;
/**
* Check if type is symbol
* Protects against any type
*/
toBeSymbol: Scolder<ExpectSymbol<Actual>, Options>;Usage Examples:
import { expectTypeOf } from "expect-type";
// Primitive type checks
expectTypeOf(42).toBeNumber();
expectTypeOf(1).toBeNumber(); // literal types work too
expectTypeOf("hello").toBeString();
expectTypeOf("literal").toBeString(); // literal strings work too
expectTypeOf(true).toBeBoolean();
expectTypeOf(false).toBeBoolean(); // both boolean literals work
expectTypeOf(123n).toBeBigInt();
expectTypeOf(Symbol("test")).toBeSymbol();
// Protects against any
const badParser = (s: string) => JSON.parse(s); // returns any
// @ts-expect-error - catches functions returning any
expectTypeOf(badParser).returns.toBeNumber();
const goodParser = (s: string): number => Number.parseInt(s, 10);
expectTypeOf(goodParser).returns.toBeNumber(); // works fineCheck for special TypeScript types including any, unknown, never, and void.
/**
* Check if type is any
* Useful for detecting overly permissive types
*/
toBeAny: Scolder<ExpectAny<Actual>, Options>;
/**
* Check if type is unknown
* Useful for type-safe unknown handling
*/
toBeUnknown: Scolder<ExpectUnknown<Actual>, Options>;
/**
* Check if type is never
* Useful for exhaustiveness checking and unreachable code
*/
toBeNever: Scolder<ExpectNever<Actual>, Options>;
/**
* Check if type is void
* Useful for function return type validation
*/
toBeVoid: Scolder<ExpectVoid<Actual>, Options>;Usage Examples:
import { expectTypeOf } from "expect-type";
// Special TypeScript types
expectTypeOf<any>().toBeAny();
expectTypeOf<unknown>().toBeUnknown();
expectTypeOf<never>().toBeNever();
// Void functions
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf(() => console.log("test")).returns.toBeVoid();
// Catch never in unreachable code
function exhaustiveCheck(value: "a" | "b"): string {
switch (value) {
case "a": return "A";
case "b": return "B";
default:
// This should be never
expectTypeOf(value).toBeNever();
throw new Error("Unreachable");
}
}
// Distinguish between any and unknown
expectTypeOf<{ deeply: { nested: any } }>()
.not.toEqualTypeOf<{ deeply: { nested: unknown } }>();Check for null, undefined, and nullable (null | undefined) types.
/**
* Check if type is null
*/
toBeNull: Scolder<ExpectNull<Actual>, Options>;
/**
* Check if type is undefined
*/
toBeUndefined: Scolder<ExpectUndefined<Actual>, Options>;
/**
* Check if type is nullable (null or undefined)
* Works with union types containing null or undefined
*/
toBeNullable: Scolder<ExpectNullable<Actual>, Options>;Usage Examples:
import { expectTypeOf } from "expect-type";
// Null and undefined
expectTypeOf(null).toBeNull();
expectTypeOf(undefined).toBeUndefined();
// Nullable types
expectTypeOf(null).toBeNullable();
expectTypeOf(undefined).toBeNullable();
expectTypeOf<string | null>().toBeNullable();
expectTypeOf<number | undefined>().toBeNullable();
expectTypeOf<boolean | null | undefined>().toBeNullable();
// Precise nullable checking
expectTypeOf(null).not.toBeUndefined();
expectTypeOf(undefined).not.toBeNull();
expectTypeOf<string>().not.toBeNullable();Check for common JavaScript data structures and objects.
/**
* Check if type is a function
* Includes all function types: regular functions, arrow functions, methods
*/
toBeFunction: Scolder<ExpectFunction<Actual>, Options>;
/**
* Check if type is an object
* Excludes arrays, functions, and null
*/
toBeObject: Scolder<ExpectObject<Actual>, Options>;
/**
* Check if type is an array
* Includes all array types and readonly arrays
*/
toBeArray: Scolder<ExpectArray<Actual>, Options>;Usage Examples:
import { expectTypeOf } from "expect-type";
// Function types
expectTypeOf(() => 1).toBeFunction();
expectTypeOf(function named() {}).toBeFunction();
expectTypeOf(Math.max).toBeFunction();
// Object types
expectTypeOf({}).toBeObject();
expectTypeOf({ a: 1, b: 2 }).toBeObject();
expectTypeOf(new Date()).toBeObject();
// Array types
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2, 3]).toBeArray();
expectTypeOf<readonly string[]>().toBeArray();
expectTypeOf<Array<number>>().toBeArray();
// Type hierarchy validation
expectTypeOf<number[]>().toBeArray();
expectTypeOf<number[]>().not.toBeObject(); // arrays are not objects in this context
expectTypeOf<() => void>().toBeFunction();
expectTypeOf<() => void>().not.toBeObject(); // functions are not objects in this contextAll basic type checkers accept types that extend the expected type.
import { expectTypeOf } from "expect-type";
// Literal types extend their base types
expectTypeOf<1>().toBeNumber(); // 1 extends number
expectTypeOf<"hello">().toBeString(); // "hello" extends string
expectTypeOf<true>().toBeBoolean(); // true extends boolean
expectTypeOf<0n>().toBeBigInt(); // 0n extends bigint
// Array subtypes
expectTypeOf<number[]>().toBeArray(); // number[] extends array
expectTypeOf<readonly string[]>().toBeArray(); // readonly arrays extend array
// Object subtypes
expectTypeOf<{ a: number; b: string }>().toBeObject(); // specific objects extend objectAll basic type checking methods support negation through .not.
import { expectTypeOf } from "expect-type";
// Negated basic type checks
expectTypeOf("hello").not.toBeNumber();
expectTypeOf(42).not.toBeString();
expectTypeOf(true).not.toBeVoid();
expectTypeOf({}).not.toBeArray();
expectTypeOf([]).not.toBeObject();
// Negated special types
expectTypeOf<string>().not.toBeAny();
expectTypeOf<string>().not.toBeUnknown();
expectTypeOf<string>().not.toBeNever();
expectTypeOf<string>().not.toBeNullable();When basic type assertions fail, expect-type provides clear error messages:
This expression is not callable.
Type 'ExpectString<number>' has no call signatures.The meaningful part is: ExpectString<number> indicates you passed a number but expected a string.