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
nameproperty in the class constructor to use this method, thenameproperty 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"));
// ^ trueCustom 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");
// ^ trueString Check
string(value: unknown): value is string
Checks if the value is a string.
Example:
const result = is.string("Hello");
// ^ trueNegative example:
const result = is.string(42);
// ^ falseNumber Check
number(value: unknown): value is number
Checks if the value is a number.
Example:
const result = is.number(42);
// ^ trueNull Check
null(value: unknown): value is null
Checks if the value is null.
Example:
const result = is.null(null);
// ^ trueUndefined Check
undefined(value: unknown): value is undefined
Checks if the value is undefined.
Example:
const result = is.undefined(undefined);
// ^ trueBoolean Check
boolean(value: unknown): value is boolean
Checks if the value is a boolean.
Example:
const result = is.boolean(true);
// ^ trueBigInt Check
bigint(value: unknown): value is bigint
Checks if the value is a bigint.
Example:
const result = is.bigint(BigInt(42));
// ^ trueSymbol Check
symbol(value: unknown): value is symbol
Checks if the value is a symbol.
Example:
const result = is.symbol(Symbol("Hello"));
// ^ trueObject Check
object(value: unknown): value is object
Checks if the value is an object.
Example:
const result = is.object({ key: "value" });
// ^ trueArray Check
array(value: unknown): value is unknown[]
Checks if the value is an array.
Example:
const result = is.array([1, 2, 3]);
// ^ trueFunction Check
function(value: unknown): value is ((...args: unknown[]) => unknown) | Function
Checks if the value is a function.
Example:
const result = is.function(() => {});
// ^ trueAny Check
any(value: unknown): value is any
Always returns true.
Example:
const result = is.any("Hello");
// ^ trueDate Check
date(value: unknown): value is Date
Checks if the value is a Date.
Example:
const result = is.date(new Date());
// ^ trueError Check
error(value: unknown): value is Error
Checks if the value is an Error.
Example:
const result = is.error(new Error("Something went wrong"));
// ^ truePromise Check
promise(value: unknown): value is Promise<unknown>
Checks if the value is a Promise.
Example:
const result = is.promise(new Promise(() => {}));
// ^ trueFalsy Check
falsy(value: unknown): value is Falsy
Checks if the value is falsy.
Example:
const result = is.falsy(null);
// ^ trueSchema 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");
// ^ trueOf 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"));
// ^ trueBrowser 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();