Reputation: 1701
I have a set of classes extending a base class. And there is a function which might be called with an argument as any of the classes above. How do I type the function here?
For example:
class BaseClass {}
class A extends BaseClass {
// ...
}
class B extends BaseClass{
}
and here is the function I want to type:
function myfunc(arg){
// do something
}
here the arg will be A or B or anything which is extended from BaseClass.
(I don't prefer giving It as A | B
, because there is a lot of classes and it is a generic function)
Upvotes: 0
Views: 20
Reputation: 45121
As you wrote "it is a generic function". So give it a generic type argument with a constraint to extend BaseClass
function myfunc<T extends BaseClass>(arg: T){
// do something
}
Upvotes: 1