Let's say I have an interface:
interface Comparable<T> {
equals(other:T):boolean
}
Which then I'm implementing in several classes:
class Rectangle implements Comparable<Rectangle> {
equals(other:Rectangle):boolean {
// logic
return true;
}
}
class Circle implements Comparable<Circle> {
equals(other:Circle):boolean {
// logic
return true;
}
}
Why TypeScript allows for comparing rectangle and circle?
let circle:Circle = new Circle();
let rectangle:Rectangle = new Rectangle();
console.log( circle.equals(rectangle) );
Shouldn't it warn me that I provided incompatible type to circle's equals method?
Generics provide the type checking at compile time .
Most container types in the Typing module are generic, as they allow you to define what type the contents of the container will be on instantiation. In the case of Dict we can state the type of the key and value.
To examine a generic type and its type parametersGet an instance of Type that represents the generic type. In the following code, the type is obtained using the C# typeof operator ( GetType in Visual Basic, typeid in Visual C++). See the Type class topic for other ways to get a Type object.
TypeScript fully supports generics as a way to introduce type-safety into components that accept arguments and return values whose type will be indeterminate until they are consumed later in your code.
Like JavaScript, TypeScript uses duck typing. So in your example rectangle and circle are identical.
Once these classes add their own implementations the duck typing will fail and the TypeScript compiler will give you errors.
class Rectangle implements Comparable<Rectangle> {
width: number;
height: number;
equals(other:Rectangle): boolean {
// logic
return true;
}
}
class Circle implements Comparable<Circle> {
diameter: number;
equals(other:Circle): boolean {
// logic
return true;
}
}
Because your Rectangle and Circle are structurally identical, TypeScript treats them as interchangable types (see "duck typing"). Just flesh out your circle and rectangle by adding some mutually incompatible properties to them:
class Rectangle implements Comparable<Rectangle> {
x: number;
equals(other:Rectangle):boolean {return true;}
}
class Circle implements Comparable<Circle> {
rad: number;
equals(other:Circle):boolean {return true;}
}
And you'll see the error show up. This is, incidentally, the same reason that you can assign an object literal to a Circle-typed var, as long as it has the right properties:
var c: Circle = {rad: 1, equals: () => true}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With