Sub topic 1
Subtyping and assignability: Dog and Animal
Prerequisites
You should know TypeScript classes, interfaces, type annotations, and basic assignability. You should also be familiar with the strict compiler option and understand that TypeScript usually compares types by their structure.
A subtype is more specific
A subtype is a more specific version of another type. The broader type is called the supertype.
In this chapter, Dog is a subtype of Animal:
class Animal {
name = "animal";
}
class Dog extends Animal {
bark() {
return "woof";
}
}
const dog: Dog = new Dog();
const animal: Animal = dog; // OK
console.log(animal.name); // animalEvery Dog has the members required by Animal. A dog has a name, because it inherits that property. It also has its own bark method. Therefore, a Dog value can be used wherever an Animal value is expected.
The reverse assignment is not safe:
class Animal {
name = "animal";
}
class Dog extends Animal {
bark() {
return "woof";
}
}
const animal: Animal = new Animal();
// Error: Animal does not have the bark method required by Dog
// const dog: Dog = animal;An Animal might be a Cat, a Horse, or another class with no bark method. Allowing the assignment would let code call bark() on an object that might not support it.
Structural typing matters
TypeScript uses structural typing. This means the compiler normally checks the members a value has instead of relying only on the names of its classes.
For example, an object with a compatible name property can be assigned to Animal:
class Animal {
name = "animal";
}
const objectAnimal: Animal = {
name: "temporary animal"
};
console.log(objectAnimal.name);The object does not need to be created with new Animal(). It only needs to have the structure required by Animal.
The same rule explains why Dog is assignable to Animal: Dog contains everything Animal requires. The extra bark method does not cause a problem. Code expecting an Animal promises to use only the Animal part of the value.
Why this direction matters for generics
Variance describes what happens when this subtype relationship appears inside a generic type. If Dog is assignable to Animal, the next question is whether a type such as Producer<Dog> can be assigned to Producer<Animal>.
The answer depends on what the generic type does with its type parameter. A type that only produces values may preserve the direction. A type that consumes values may reverse it. A type that both produces and consumes values may allow neither direction.
Keep the basic relationship clear:
Dogis more specific thanAnimal.- A
Dogcan be used as anAnimal. - An
Animalcannot automatically be used as aDog. - Extra members on the source value are allowed when the target requires fewer members.
Exercise
Why is const animal: Animal = new Dog() safe, while const dog: Dog = new Animal() is unsafe?
Show answer
Every Dog has the members required by Animal, so code using animal can safely use the Animal members. An Animal is not guaranteed to have Dog's bark method, so code using dog could make an invalid call.