Saturday, April 13, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  189] [ 2]  / answers: 1 / hits: 14601  / 8 Years ago, sat, april 23, 2016, 12:00:00

I have an interface which is extending another one. Now I am trying to put a variable of the superinterface type into a function that needs a subinterface type parameter. This is not possible as the superinterface typed variable is missing certain attributes.

It's a little hard to explain, so I've created an example with an Animal interface, which is extended by a Horse interface:

interface Animal {
//... 100 other things

interface Horse extends Animal {

Now I have a module with one private function (rideHorse), which only accepts a Horse as parameter. And a public function (rideAnimal), accepting all Animals like below:

module AnimalModule {
function rideHorse(horse:Horse) {

export function rideAnimal(animal:Animal) {
animal.speed = 10; // Error: Animal cannot have speed
rideHorse(animal); // Error: 'animal' needs speed

// Calling the rideAnimal function
var myAnimal:Animal = {
age: 10

As you can see this is not working because the animal parameter of rideAnimal doesn't have speed. So my question is: How can I cast my animal into a Horse and add speed manually inside the rideAnimal function, so the errors will disappear?

More From » types


You can cast your Animal into a Horse like this.

function rideAnimal(animal:Animal) {
var horse = animal as Horse;
horse.speed = 10;

[#29236] Thursday, April 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 6
Total Questions: 100
Total Answers: 97

Location: England
Member since Sun, May 21, 2023
12 Months ago