Is Class

The Is class provides a set of utility methods for type checking in TypeScript. It includes checks for basic types like string, number, null, undefined, boolean, bigint, symbol, object, function, any, array, date, error, promise, falsy, and custom checks using Zod schemas. Additionally, it provides methods for checking the execution environment (browser or server).

Usage

Importing the Is Class

import { Is } from "@mvdlei/tzod/is";

Creating an Instance

const is = new Is();

Or get from the singleton t instance:

import { t } from "@mvdlei/tzod";
 
const is = t.is;

Methods

Instanceof Check

Be sure to call the class with new before passing it to the instanceof method.

instanceof<T>(constructor: new (...args: any[]) => T, value: unknown): value is T

Checks if the value is an instance of a class.

Note: Make sure to set the name property in the class constructor to use this method, the name property is used to check the class name.

Example:

class MyClass {
  constructor(public readonly name: string) {}
}
 
const result = is.instanceof(MyClass, new MyClass("Instance of MyClass"));
// ^ true

Custom Check with of

of<T>(constructor: T, func: (value: T) => value is T): value is T

Checks if the value passes a custom check.

Example:

const result = is.of("Hello", (value) => value === "Hello");
// ^ true

String Check

string(value: unknown): value is string

Checks if the value is a string.

Example:

const result = is.string("Hello");
// ^ true

Negative example:

const result = is.string(42);
// ^ false

Number Check

number(value: unknown): value is number

Checks if the value is a number.

Example:

const result = is.number(42);
// ^ true

Null Check

null(value: unknown): value is null

Checks if the value is null.

Example:

const result = is.null(null);
// ^ true

Undefined Check

undefined(value: unknown): value is undefined

Checks if the value is undefined.

Example:

const result = is.undefined(undefined);
// ^ true

Boolean Check

boolean(value: unknown): value is boolean

Checks if the value is a boolean.

Example:

const result = is.boolean(true);
// ^ true

BigInt Check

bigint(value: unknown): value is bigint

Checks if the value is a bigint.

Example:

const result = is.bigint(BigInt(42));
// ^ true

Symbol Check

symbol(value: unknown): value is symbol

Checks if the value is a symbol.

Example:

const result = is.symbol(Symbol("Hello"));
// ^ true

Object Check

object(value: unknown): value is object

Checks if the value is an object.

Example:

const result = is.object({ key: "value" });
// ^ true

Array Check

array(value: unknown): value is unknown[]

Checks if the value is an array.

Example:

const result = is.array([1, 2, 3]);
// ^ true

Function Check

function(value: unknown): value is ((...args: unknown[]) => unknown) | Function

Checks if the value is a function.

Example:

const result = is.function(() => {});
// ^ true

Any Check

any(value: unknown): value is any

Always returns true.

Example:

const result = is.any("Hello");
// ^ true

Date Check

date(value: unknown): value is Date

Checks if the value is a Date.

Example:

const result = is.date(new Date());
// ^ true

Error Check

error(value: unknown): value is Error

Checks if the value is an Error.

Example:

const result = is.error(new Error("Something went wrong"));
// ^ true

Promise Check

promise(value: unknown): value is Promise<unknown>

Checks if the value is a Promise.

Example:

const result = is.promise(new Promise(() => {}));
// ^ true

Falsy Check

falsy(value: unknown): value is Falsy

Checks if the value is falsy.

Example:

const result = is.falsy(null);
// ^ true

Schema Check

schema(schema: ZodTypeAny, value: unknown): value is ZodTypeAny

Checks if the value adheres to a Zod schema.

Example:

import { z } from "zod";
 
const schema = z.string();
const result = is.schema(schema, "Hello");
// ^ true

Of Check

of<T>(constructor: T, value: unknown): value is T

Checks if the value is an instance of a class or object.

Example:

class MyClass {
  constructor(public readonly name: string) {}
}
 
const result = is.of(MyClass, new MyClass("Instance of MyClass"));
// ^ true

Browser Check

browser(): boolean

Checks if the code is running in a browser.

Example:

const result = is.browser();

Server Check

server(): boolean

Checks if the code is running without a browser.

Example:

const result = is.server();