I have a base class, Component:
module abc {
export class Component {}
}
I also have various classes extending this base class:
module cde {
export class Position extends abc.Component {}
}
Now, there’s an entity class that pretty much just contains components:
module abc {
export class Entity {
add(component: Component) {}
}
}
Why can I not then do the following:
var entity = new abc.Entity().add(new cde.Position());
The compiler complains that Argument types do not match parameters even though my Position class extends the Component class which is the type that the add method expects…
Changing that to
var entity = new abc.Entity().add(<abc.Component>new cde.Position());
satisfies the compilers error but I don’t understand why I would need to explicitly downcast like that..
After reading Breeze’s answer I pulled out my actual code, snipped irrelevant parts, and pasted it into playground… Low and behold the code actually works. Maybe it’s an issue with IntelliJ’s implementation of typescript? I’ll keep investigating.
I tried the following code :
And the compiler does not complain…
Two questions,
What version of the compiler are you using?
Is this your exact code? (since your add method does not return anything, but you assign it to ‘var entity’)