To Class
The To
class provides a set of utility methods for type casting in TypeScript. It includes casts for basic types like string, number, boolean, bigint, symbol, object, function, any, array, date, error, promise, and custom casts using Zod schemas.
Usage
Importing the To Class
Creating an Instance
Or get from the singleton t
instance:
Methods
String Cast
string(value: unknown): string
Casts the value to a string.
Example:
Number Cast
number(value: unknown): number
Casts the value to a number.
Example:
Boolean Cast
boolean(value: unknown): boolean
Casts the value to a boolean.
Example:
BigInt Cast
bigint(value: unknown): bigint
Casts the value to a bigint.
Example:
Symbol Cast
symbol(value: unknown): symbol
Casts the value to a symbol.
Example:
Url Cast
url(value: unknown): URL
Casts the value to a URL.
Example:
Promise Cast
promise<T>(value: unknown): Promise<T>
Casts the value to a Promise.
Example:
Date Cast
date(value: unknown): Date
Casts the value to a Date.
Example:
Error Cast
error(value: unknown): Error
Casts the value to an Error.
Example:
Readonly Cast
readonly<T>(value: T): Readonly<T>
Casts the value to a Readonly.
Example:
Array Cast
array<T>(value: unknown): T[]
Casts the value to an array.
Example: