Reputation: 50777
I'm moving to Typescript for a variety of reasons, and so far I'm supremely happy with it.
One of the issues that I've ran into is verifying that an argument passed to a function extends another class.
For instance:
class Foo {
public $xyz
}
class Bar {
constructor(model: extends Foo)
}
class Baz extends Foo {}
Many classes can extend foo, besides Baz, but they all have the same properties. I need to ensure that the class being passed to the model
argument of the constructor Bar
correctly extends Foo
.
How can I achieve this?
(please forgive the terrible psuedo-code above)
If my approach is wrong, I'd love to hear what I could do to solve that problem as well.
Upvotes: 1
Views: 471
Reputation: 62213
constructor(model: Foo)
Is what you are looking for. This states that the passed instance model
is of type Foo
or inherits from Foo
.
The keyword extends
can only be used in type declarations to denote that the type extends (inherits from) another type.
See also Classes documentation.
Upvotes: 1