typesafe-ts API Documentation - v1.7.0
    Preparing search index...

    Function Assert

    • 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.

      Type Parameters

      • T extends
            | true
            | {
                "Assertion Error": AssertMessage;
                "You provided": T;
                Hint: T extends false
                    ? "The asserted condition is false."
                    : "Assert works best with conditional types that resolves to true or false, such as Check.Equal<A, B>, Check.IsNever<T>, etc.";
            }

        Should be a type that resolves to true from Check utilities. If false, compilation fails.

      • AssertMessage extends string = ""

        Optional custom error message displayed when assertion fails

      Returns T extends true ? void : AssertionError<T, AssertMessage>

      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">();
      • import("./check.ts").Equal - Test if two types are exactly equal
      • import("./check.ts").True - Test if a type is exactly true
      • import("./check.ts").False - Test if a type is exactly false
      • import("./check.ts").Extends - Test if one type extends another
      • import("./check.ts").IsNever - Test if a type is never
      • import("./check.ts").IsAny - Test if a type is any
      • import("./check.ts").IsUnknown - Test if a type is unknown