Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
A generic class in TypeScript is a class definition parameterized over one or more types, allowing the encapsulation of logic and data structures that operate uniformly across different data types while maintaining strict compile-time type safety. Type parameters are declared in angle brackets (<T>) immediately following the class identifier and are scoped to the instance of the class.
Syntax
class ClassName<T> {
property: T;
constructor(arg: T) {
this.property = arg;
}
method(): T {
return this.property;
}
}
Core Mechanics
Type Parameter Declaration
Type parameters act as placeholders for concrete types. They are defined at the class level and can be utilized within property declarations, constructor parameters, method signatures, and method bodies.
class KeyValuePair<K, V> {
constructor(public key: K, public value: V) {}
public getPair(): [K, V] {
return [this.key, this.value];
}
}
Instantiation and Type Inference
When instantiating a generic class, type arguments can be passed explicitly. If omitted, the TypeScript compiler will attempt to infer the type arguments based on the values passed to the constructor.
// Explicit type assignment
const explicitPair = new KeyValuePair<string, number>("id", 100);
// Implicit type inference (K inferred as string, V inferred as boolean)
const inferredPair = new KeyValuePair("isActive", true);
Advanced Characteristics
Generic Constraints
Type parameters can be constrained using the extends keyword to enforce that the provided type implements a specific interface or inherits from a specific base class. This guarantees the presence of specific properties or methods on the generic type.
interface HasLength {
length: number;
}
class LengthValidator<T extends HasLength> {
constructor(private item: T) {}
public isValid(minLength: number): boolean {
return this.item.length >= minLength;
}
}
Default Type Parameters
Type parameters can be assigned default types using the = operator. If a type argument is not provided during instantiation and cannot be inferred from the constructor arguments, the compiler will fall back to the default type.
class DataWrapper<T = string> {
// Parameter is marked as optional (?) so the class can be instantiated without arguments
constructor(public data?: T) {}
}
// T resolves to string via the default type parameter
const defaultWrapper = new DataWrapper();
Static Member Limitations
Static properties and methods belong to the class constructor function itself, not to any specific instance. Consequently, static members cannot reference class-level type parameters.
class GenericClass<T> {
// ERROR: Static members cannot reference class type parameters.
// static staticProperty: T;
// Valid: Static methods can declare their own independent type parameters.
static staticMethod<U>(arg: U): U {
return arg;
}
}
Master TypeScript with Deep Grasping Methodology!Learn More