TypeScript's largest utility library providing 200+ type utilities for advanced type manipulation.
—
Miscellaneous utilities including built-in type definitions, primitive types, and JSON handling capabilities.
Definitions for TypeScript's built-in object types.
/**
* Union of TypeScript's built-in object types
*/
type BuiltIn = Function | Error | Date | { readonly [Symbol.toStringTag]: string } | RegExp | Generator;Usage Examples:
import { Misc } from "ts-toolbelt";
// Check if type extends built-in
type IsBuiltIn<T> = T extends Misc.BuiltIn ? true : false;
type DateCheck = IsBuiltIn<Date>; // true
type ErrorCheck = IsBuiltIn<Error>; // true
type RegExpCheck = IsBuiltIn<RegExp>; // true
type FunctionCheck = IsBuiltIn<Function>; // true
type CustomCheck = IsBuiltIn<{ name: string }>; // false
// Filter built-ins from union
type Mixed = Date | string | RegExp | number | Error;
type OnlyBuiltIns = Extract<Mixed, Misc.BuiltIn>; // Date | RegExp | Error
type NoBuiltIns = Exclude<Mixed, Misc.BuiltIn>; // string | numberDefinition of JavaScript primitive types.
/**
* Union of JavaScript primitive types
*/
type Primitive = string | number | boolean | bigint | symbol | undefined | null;Usage Examples:
import { Misc } from "ts-toolbelt";
// Check if type is primitive
type IsPrimitive<T> = T extends Misc.Primitive ? true : false;
type StringCheck = IsPrimitive<string>; // true
type NumberCheck = IsPrimitive<number>; // true
type BooleanCheck = IsPrimitive<boolean>; // true
type BigIntCheck = IsPrimitive<bigint>; // true
type SymbolCheck = IsPrimitive<symbol>; // true
type NullCheck = IsPrimitive<null>; // true
type UndefinedCheck = IsPrimitive<undefined>; // true
type ObjectCheck = IsPrimitive<{}>; // false
type ArrayCheck = IsPrimitive<[]>; // false
type FunctionCheck = IsPrimitive<() => void>; // false
// Filter primitives from complex types
type ComplexUnion = string | { name: string } | number | Date | boolean;
type OnlyPrimitives = Extract<ComplexUnion, Misc.Primitive>; // string | number | boolean
type OnlyObjects = Exclude<ComplexUnion, Misc.Primitive>; // { name: string } | DateSpecialized types for working with JSON data structures.
/**
* JSON namespace containing JSON-specific type utilities
*/
namespace JSON {
/**
* Valid JSON primitive values
*/
type Primitive = string | number | boolean | null;
/**
* JSON array type (recursive)
*/
type Array = Value[];
/**
* JSON object type (recursive)
*/
type Object = { [key: string]: Value };
/**
* Any valid JSON value
*/
type Value = Primitive | Object | Array;
}Usage Examples:
import { Misc } from "ts-toolbelt";
// JSON type validation
type ValidJsonValue<T> = T extends Misc.JSON.Value ? T : never;
type ValidString = ValidJsonValue<string>; // string
type ValidNumber = ValidJsonValue<number>; // number
type ValidBoolean = ValidJsonValue<boolean>; // boolean
type ValidNull = ValidJsonValue<null>; // null
type ValidObject = ValidJsonValue<{ name: string; age: number }>; // { name: string; age: number }
type ValidArray = ValidJsonValue<string[]>; // string[]
// Invalid JSON types
type InvalidUndefined = ValidJsonValue<undefined>; // never
type InvalidFunction = ValidJsonValue<() => void>; // never
type InvalidSymbol = ValidJsonValue<symbol>; // never
type InvalidBigInt = ValidJsonValue<bigint>; // never
// JSON-safe object creation
type JsonSafeObject<T> = {
[K in keyof T]: T[K] extends Misc.JSON.Value ? T[K] : never;
};
type User = {
name: string;
age: number;
isActive: boolean;
callback: () => void; // Invalid in JSON
metadata: { created: Date }; // Date invalid in JSON
};
type SafeUser = JsonSafeObject<User>;
// {
// name: string;
// age: number;
// isActive: boolean;
// callback: never;
// metadata: never;
// }
// API response typing
type ApiResponse<T extends Misc.JSON.Value> = {
success: boolean;
data: T;
timestamp: number;
meta: Misc.JSON.Object;
};
type UserResponse = ApiResponse<{
id: string;
name: string;
preferences: {
theme: "light" | "dark";
notifications: boolean;
};
}>;
// JSON parsing type safety
type ParseResult<T extends string> = T extends `${infer _}`
? Misc.JSON.Value
: never;
// Configuration management
type Config = {
database: {
host: string;
port: number;
ssl: boolean;
};
features: {
auth: boolean;
logging: boolean;
};
metadata: Misc.JSON.Object;
};
type IsConfigJsonSafe = Config extends { [K in keyof Config]: Misc.JSON.Value } ? true : false; // true
// Deep JSON validation
type DeepJsonSafe<T> = T extends Misc.JSON.Primitive
? T
: T extends (infer U)[]
? DeepJsonSafe<U>[]
: T extends Record<string, any>
? { [K in keyof T]: DeepJsonSafe<T[K]> }
: never;
type NestedConfig = {
app: {
name: string;
version: string;
settings: {
debug: boolean;
timeout: number;
};
};
};
type SafeNestedConfig = DeepJsonSafe<NestedConfig>; // Same as NestedConfig (all JSON-safe)Real-world usage patterns for miscellaneous utilities.
Usage Examples:
import { Misc, A } from "ts-toolbelt";
// Serialization type checking
type Serializable<T> = T extends Misc.Primitive
? T
: T extends Misc.BuiltIn
? never
: T extends (infer U)[]
? Serializable<U>[]
: T extends Record<string, any>
? { [K in keyof T]: Serializable<T[K]> }
: never;
type Data = {
id: string;
createdAt: Date; // Not serializable
tags: string[];
metadata: { version: number };
};
type SerializableData = Serializable<Data>;
// {
// id: string;
// createdAt: never;
// tags: string[];
// metadata: { version: number };
// }
// Environment configuration validation
type EnvValue = string | number | boolean;
type EnvConfig = Record<string, EnvValue>;
type IsEnvSafe<T> = T extends EnvValue ? true : false;
type DatabaseConfig = {
DB_HOST: string;
DB_PORT: number;
DB_SSL: boolean;
DB_POOL: { min: number; max: number }; // Not env-safe (object)
};
type EnvSafeCheck = {
[K in keyof DatabaseConfig]: IsEnvSafe<DatabaseConfig[K]>;
};
// {
// DB_HOST: true;
// DB_PORT: true;
// DB_SSL: true;
// DB_POOL: false;
// }
// Type filtering utilities
type FilterPrimitives<T> = T extends Misc.Primitive ? T : never;
type FilterBuiltIns<T> = T extends Misc.BuiltIn ? T : never;
type FilterJsonValues<T> = T extends Misc.JSON.Value ? T : never;
type MixedTypes = string | Date | number | RegExp | boolean | { id: string } | null;
type OnlyPrimitives = FilterPrimitives<MixedTypes>; // string | number | boolean | null
type OnlyBuiltIns = FilterBuiltIns<MixedTypes>; // Date | RegExp
type OnlyJsonValues = FilterJsonValues<MixedTypes>; // string | number | boolean | { id: string } | null// Core miscellaneous types
type BuiltIn = Function | Error | Date | { readonly [Symbol.toStringTag]: string } | RegExp | Generator;
type Primitive = string | number | boolean | bigint | symbol | undefined | null;
// JSON namespace types
namespace JSON {
type Primitive = string | number | boolean | null;
type Array = Value[];
type Object = { [key: string]: Value };
type Value = Primitive | Object | Array;
}Install with Tessl CLI
npx tessl i tessl/npm-ts-toolbelt