Sohnee Sohnee - 3 months ago 41
TypeScript Question

Can You Specify Multiple Type Constraints For TypeScript Generics

I have a generic interface like this example with a single type constraint:

export interface IExample<T extends MyClass> {
getById(id: number): T;
}


Is it possible to specify multiple type constraints instead of just one?

Answer

A work around for this would be to use a super-interface (which also answers the question "why would you allow an interface to inherit from a class").

interface ISuperInterface extends MyClass, OtherClass {

}

export interface IExample<T extends ISuperInterface> {
    getById(id: number): T;
}