You do not need generics for this. Generics is for creating different types of functions out of your generic type. Here you are only looking for a certain type of function that only accepts instances of B.
Also you better define some properties in your classes otherwise all empty class instance will be assignable to empty class instances. This github issue has more info. The reason seems to be Structural Type system.
A structural type system (or property-based type system) is a major class of type systems in which type compatibility and equivalence are determined by the type's actual structure or definition and not by other characteristics such as its name or place of declaration.
Here is the wiki for this
You can look at this to understand:
class A {
private a: number
}
class B extends A {
private b: number
}
class C extends B {
private c: number
}
function f(x : B): any {
}
const a = f(new A()); // should complain
const b = f(new B());
const c = f(new C());
const d = f(123); // should complain
Link (Please ignore the non-use warning in the playground. That is not related to the question)
Notice how if you remove the properties from the classes, the errors disappear.
PS: You can still use generics here like:
class A {
private a: number
}
class B extends A {
private b: number
}
class C extends B {
private c: number
}
function f<P extends B>(x : P): any {
}
const a = f(new A()); // should complain
const b = f(new B());
const c = f(new C());
const d = f(123); // should complain
Link