TypeScript's largest utility library providing 200+ type utilities for advanced type manipulation.
—
Class-related utilities for extracting instance types, constructor parameters, and working with class constructors in TypeScript's type system.
Base class type representing constructor functions.
/**
* Generic class constructor type
* @param A - Instance type that the class creates
* @param P - Constructor parameter types as tuple
*/
type Class<A = any, P extends readonly any[] = any> = new (...args: P) => A;Usage Examples:
import { C } from "ts-toolbelt";
// Basic class type
type StringClass = C.Class<string, [string]>; // new (value: string) => string
type NumberClass = C.Class<number, [number]>; // new (value: number) => number
// Complex class constructor
type UserClass = C.Class<
{ name: string; age: number },
[string, number]
>; // new (name: string, age: number) => { name: string; age: number }
// Generic class
type GenericClass<T> = C.Class<T[], [T[]]>; // new (items: T[]) => T[]
type StringArrayClass = GenericClass<string>; // new (items: string[]) => string[]Extract the instance type that a class constructor creates.
/**
* Extract instance type from class constructor
* @param C - Class constructor to extract from
* @returns Instance type created by the constructor
*/
type Instance<C extends Class> = C extends Class<infer I> ? I : never;Usage Examples:
import { C } from "ts-toolbelt";
// Built-in classes
type DateInstance = C.Instance<typeof Date>; // Date
type ArrayInstance = C.Instance<typeof Array>; // any[]
type MapInstance = C.Instance<typeof Map>; // Map<any, any>
// Custom classes (conceptual - would work with actual class definitions)
declare class User {
name: string;
age: number;
constructor(name: string, age: number);
}
type UserInstance = C.Instance<typeof User>; // User (the instance type)
// Generic classes
declare class Container<T> {
value: T;
constructor(value: T);
}
type StringContainer = C.Instance<typeof Container<string>>; // Container<string>
type NumberContainer = C.Instance<typeof Container<number>>; // Container<number>
// Abstract pattern
type SomeClass = C.Class<{ id: string; value: number }, [string, number]>;
type ExtractedInstance = C.Instance<SomeClass>; // { id: string; value: number }Extract parameter types from class constructors.
/**
* Extract constructor parameter types from class
* @param C - Class constructor to extract parameters from
* @returns Tuple of constructor parameter types
*/
type Parameters<C extends Class> = C extends Class<any, infer P> ? P : never;Usage Examples:
import { C } from "ts-toolbelt";
// Built-in class parameters
type DateParams = C.Parameters<typeof Date>; // [string | number | Date] | [] | [number, number, number, ...] (overloaded)
type ArrayParams = C.Parameters<typeof Array>; // [number] | any[] (overloaded)
// Custom class parameters
declare class Product {
constructor(name: string, price: number, inStock?: boolean);
}
type ProductParams = C.Parameters<typeof Product>; // [string, number, boolean?]
// No parameters
declare class Singleton {
constructor();
}
type SingletonParams = C.Parameters<typeof Singleton>; // []
// Rest parameters
declare class Collection<T> {
constructor(...items: T[]);
}
type CollectionParams = C.Parameters<typeof Collection<string>>; // [string, ...string[]]
// Complex parameter patterns
declare class Database {
constructor(
config: { host: string; port: number },
options?: { ssl: boolean; timeout: number }
);
}
type DatabaseParams = C.Parameters<typeof Database>;
// [{ host: string; port: number }, { ssl: boolean; timeout: number }?]Real-world usage patterns for class type utilities.
Usage Examples:
import { C, F } from "ts-toolbelt";
// Factory pattern
type Factory<T extends Class> = {
create: (...args: C.Parameters<T>) => C.Instance<T>;
createMultiple: (count: number, ...args: C.Parameters<T>) => C.Instance<T>[];
};
declare class User {
constructor(name: string, email: string);
}
type UserFactory = Factory<typeof User>;
// {
// create: (name: string, email: string) => User;
// createMultiple: (count: number, name: string, email: string) => User[];
// }
// Repository pattern
type Repository<T extends Class> = {
entity: T;
create: (...args: C.Parameters<T>) => Promise<C.Instance<T>>;
findById: (id: string) => Promise<C.Instance<T> | null>;
update: (id: string, data: Partial<C.Instance<T>>) => Promise<C.Instance<T>>;
delete: (id: string) => Promise<boolean>;
};
type UserRepository = Repository<typeof User>;
// Repository with User-specific types
// Dependency injection
type Injectable<T extends Class> = {
token: symbol;
useClass: T;
scope: 'singleton' | 'transient';
};
type Provider<T extends Class> = {
provide: symbol;
useFactory: (...deps: any[]) => C.Instance<T>;
inject: symbol[];
};
declare class ApiService {
constructor(httpClient: any, config: { baseUrl: string });
}
type ApiServiceInjectable = Injectable<typeof ApiService>;
type ApiServiceProvider = Provider<typeof ApiService>;
// Mixin pattern
type Constructor<T = {}> = C.Class<T>;
type Mixin<T extends Constructor> = (Base: T) => T;
declare class BaseEntity {
id: string;
constructor(id: string);
}
type TimestampMixin = Mixin<typeof BaseEntity>;
// Mixin that adds timestamp functionality
// Validation
type ValidateConstructor<T extends Class> = {
validate: (...args: C.Parameters<T>) => boolean;
create: (...args: C.Parameters<T>) => C.Instance<T>;
};
type ValidatedUser = ValidateConstructor<typeof User>;
// {
// validate: (name: string, email: string) => boolean;
// create: (name: string, email: string) => User;
// }
// Builder pattern
type Builder<T extends Class> = {
[K in keyof C.Instance<T>]: (value: C.Instance<T>[K]) => Builder<T>;
} & {
build: () => C.Instance<T>;
};
declare class Config {
host: string;
port: number;
ssl: boolean;
constructor(host: string, port: number, ssl: boolean);
}
type ConfigBuilder = Builder<typeof Config>;
// {
// host: (value: string) => ConfigBuilder;
// port: (value: number) => ConfigBuilder;
// ssl: (value: boolean) => ConfigBuilder;
// build: () => Config;
// }
// Class registry
type ClassRegistry = {
[key: string]: Class;
};
type RegistryInstance<R extends ClassRegistry, K extends keyof R> =
C.Instance<R[K]>;
type RegistryParams<R extends ClassRegistry, K extends keyof R> =
C.Parameters<R[K]>;
type MyRegistry = {
user: typeof User;
config: typeof Config;
};
type UserFromRegistry = RegistryInstance<MyRegistry, 'user'>; // User
type ConfigFromRegistry = RegistryInstance<MyRegistry, 'config'>; // Config
type UserParamsFromRegistry = RegistryParams<MyRegistry, 'user'>; // [string, string]// Core class type representing constructor functions
type Class<A = any, P extends readonly any[] = any> = new (...args: P) => A;Install with Tessl CLI
npx tessl i tessl/npm-ts-toolbelt