Skip to main content

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 interface in TypeScript is a structural contract that accepts one or more type parameters, deferring the declaration of concrete types for its properties, methods, or index signatures until the interface is referenced, implemented, or extended. Type parameters act as placeholders, enabling strict type-checking across object shapes without hardcoding specific types.

Syntax Anatomy

Type parameters are declared within angle brackets (<>) immediately following the interface identifier.
interface GenericInterface<T, U> {
    value: T;
    transform(input: U): T;
}

Core Mechanics

1. Interface-Level Type Parameters When a type parameter is attached to the interface declaration, it is scoped to the entire interface body. Any reference to the interface must supply the required type arguments, binding the provided types to all occurrences of the parameters within the interface.
interface Container<T> {
    item: T;
    getItem(): T;
}

// Type argument 'number' is explicitly bound to T
const numberContainer: Container<number> = { 
    item: 42,
    getItem() { return this.item; }
};
2. Method-Level Type Parameters Interfaces can define generic methods where the type parameter is scoped exclusively to the method signature, independent of the interface itself.
interface Processor {
    // T is scoped only to the process method, not the Processor interface
    process<T>(payload: T): T;
}
3. Type Constraints (extends) Type parameters can be constrained using the extends keyword to enforce that the provided type argument satisfies a specific structural requirement. If the constraint is not met, the TypeScript compiler throws a type error.
interface Lengthwise {
    length: number;
}

// T is constrained; it must possess a 'length' property of type 'number'
interface MeasuredCollection<T extends Lengthwise> {
    collection: T;
    getMetrics(): number;
}
4. Default Type Parameters Type parameters can be assigned default types using the = operator. If a type argument is omitted during instantiation, TypeScript falls back to the default type. Default type parameters must appear sequentially after all required type parameters.
interface Configuration<T = string, U = boolean> {
    endpoint: T;
    secure: U;
}

// Implicitly resolves to Configuration<string, boolean>
const defaultConfig: Configuration = { 
    endpoint: "api.local", 
    secure: true 
};
5. Generic Index Signatures Generic type parameters can dictate the return type of an index signature, allowing dynamic property keys to map to a strictly typed generic value.
interface Dictionary<T> {
    [key: string]: T;
}

// All string keys must map to a boolean value
const flags: Dictionary<boolean> = {
    isActive: true,
    isHidden: false
};
6. Extending Generic Interfaces Generic interfaces can extend other generic or non-generic interfaces. When extending, the child interface can pass its own type parameters up to the parent, supply concrete types to the parent, or introduce new type parameters.
interface BaseEntity<T> {
    id: T;
}

// Passes type parameter U to parent parameter T
interface UserEntity<U> extends BaseEntity<U> {
    username: string;
}

// Supplies a concrete type (string) to parent parameter T
interface SessionEntity extends BaseEntity<string> {
    token: string;
}
Master TypeScript with Deep Grasping Methodology!Learn More