I have a super class, such as
class SuperClassy {
}
Which has many other classes extending it:
class SubClassy extends SuperClassy {
}
class OtherSubClass extends SuperClassy {
}
etc.
I have functions for which I want to specify the argument must be in this group of derived classes.
I don't want to have to create a union type manually, I'd rather just declare that the argument in question must inherit from SuperClassy.
Something like:
function doStuff(c: InheritsFrom<SuperClassy>) {
}
such that this will compile:
doStuff(new SubClassy()) {
}
and this will not:
doStuff(4);
-- EDIT --
Here is an example where simply requiring parameter to be SuperClassy does not work:
class SuperClassy {
id: string;
}
class SubClassy extends SuperClassy {
x: number;
}
class OtherSubClass extends SuperClassy {
y: number;
}
function doStuff(c: (c: SuperClassy) => (d: SuperClassy) => boolean) {
return 'j';
}
doStuff((c: SubClassy) => {
return (k: OtherSubClass) => {
return c.id === k.id;
}
});
Compiler throws this error:
Argument of type '(c: SubClassy) => (k: OtherSubClass) => boolean' is not assignable to parameter of type '(c: SuperClassy) => (d: SuperClassy) => boolean'.
Types of parameters 'c' and 'c' are incompatible.
Property 'x' is missing in type 'SuperClassy' but required in type 'SubClassy'.
function doStuff(c: SuperClassy)?doStuffis not a subtype of the parameter type defined fordoStuff. i.e.(c: SubClassy) => ...is not a subtype of(c: SuperClassy) => ...it's actually the opposite.