TypeScript Generics Generics Constraints


Example

Simple constraint:

interface IRunnable {
    run(): void;
}

interface IRunner<T extends IRunnable> {
    runSafe(runnable: T): void;
}

More complex constraint:

interface IRunnble<U> {
    run(): U;
}

interface IRunner<T extends IRunnable<U>, U> {
    runSafe(runnable: T): U;
}

Even more complex:

interface IRunnble<V> {
    run(parameter: U): V;
}

interface IRunner<T extends IRunnable<U, V>, U, V> {
    runSafe(runnable: T, parameter: U): V;
}

Inline type constraints:

interface IRunnable<T extends { run(): void }> {
    runSafe(runnable: T): void;
}