Function assertInstanceOf

  • Make an assertion that obj is an instance of type. If not then throw.

    Type Parameters

    • T extends (new (...args: any[]) => any)

      The expected type of the object.

    Parameters

    • actual: unknown

      The object to check.

    • expectedType: T

      The expected class constructor.

    • msg: string = ""

      The optional message to display if the assertion fails.

    Returns asserts actual is InstanceType<T>

    import { assertInstanceOf } from "@std/assert";

    assertInstanceOf(new Date(), Date); // Doesn't throw
    assertInstanceOf(new Date(), Number); // Throws