Should be a type that resolves to true from Check utilities. If false, compilation fails.
Optional custom error message displayed when assertion fails
Void if assertion passes, otherwise causes a compile-time error
Basic usage with Check utilities:
import { Assert, type Check } from "typesafe-ts/assert";
// ✅ These compile successfully
Assert<Check.Equal<string, string>, "Strings should be equal">();
Assert<Check.True<true>, "This should be true">();
Assert<Check.Extends<"hello", string>, "String literal extends string">();
// ❌ These cause compile errors
Assert<Check.Equal<string, number>, "Different types">(); // Compile error!
Assert<Check.True<false>, "This is false">(); // Compile error!
// ⚠️These are undefined behavior and should be avoided
// prefer Assert<Check.IsNever<T>, "...">(); for reliable behavior
Assert<never, "Behavior is not guaranteed and could flip without a breaking change.">();
// prefer Assert<Check.IsAny<T>, "...">(); for reliable behavior
Assert<any, "Behavior is not guaranteed and could flip without a breaking change.">();
Testing complex type relationships:
import { Assert, type Check } from "typesafe-ts/assert";
type User = { name: string; age: number };
type UserWithId = User & { id: string };
// Assert that UserWithId extends User
Assert<Check.Extends<UserWithId, User>, "UserWithId should extend User">();
// Assert that two interface definitions are equivalent
type ApiUser = { name: string; age: number };
Assert<Check.Equal<User, ApiUser>, "User and ApiUser should be the same">();
Detecting problematic types:
import { Assert, type Check } from "typesafe-ts/assert";
// Ensure a type is not 'any' (helps catch type safety issues)
function processValue<T>(value: T): T {
Assert<Check.IsAny<T>, "Parameter should not be 'any'">(); // ❌ Compile error if T is any
return value;
}
// Ensure a type is not 'never' (helps catch impossible type scenarios)
type Result<T> = T extends string ? string : never;
Assert<Check.IsNever<Result<number>>, "Result<number> creates never type">();
truefalseneveranyunknown
Static type assertion function that enforces compile-time type checking.
Use this function with Check utilities to assert type relationships at compile time. If the assertion fails, TypeScript will show a compile error with your custom message.