Is it possible to have interface in Typescript with generic methods that will allow us to tell that T type will extend some other type?
interface Abc {
a: string;
b: string;
c: string;
}
interface Checkout {
onComplete<T>(session: T): void
}
class StripeCheckout implements Checkout {
onComplete<T extends Abc>(abc: T) {
console.log(abc.a);
}
}
onComplete method is invalid
Property 'onComplete' in type 'StripeCheckout' is not assignable to the same property in base type 'Checkout'.
Type '<T extends Abc>(session: T) => void' is not assignable to type '<T>(session: T) => void'.
Types of parameters 'session' and 'session' are incompatible.
Type 'T' is not assignable to type 'Abc'.(2416)
Solution #1 (generic interface which will force us to tell types at the class level):
interface Abc {
a: string;
b: string;
c: string;
}
interface Checkout<T> {
onComplete(session: T): void
}
class StripeCheckout implements Checkout<Abc> {
onComplete(session: Abc) {
console.log(session);
}
}
Problem is that if I will have more generic methods with different types it will look something like this Abc<T, U, V, Z>. We could fix that with multiple interfaces, however I'd like to tell Typescript about generic method type when I define method in class.
Solution #2 ("generic" with any):
interface Abc {
a: string;
b: string;
c: string;
}
interface Checkout {
onComplete(session: any): void
}
class StripeCheckout implements Checkout {
onComplete(session: Abc) {
console.log(session);
}
}
Example usage:
const checkout = new StripeCheckout();
checkout.onComplete({a: '1', b: '2', c: '3'})
Checkout<A, B, C, D>where A,B,C,D are arguments of extended methods?