Preemptive apologies if I'm abusing the term, but I'm able to implement an interface in typescript which I believe is not type safe, for example:
interface Point {
x: number;
y: number;
dist (other: Point): number
}
GridPoint implements Point {
constructor (public x: number, public x: number) {}
dist (other: Point) { /* ... math here ... */ } //
}
NamedPoint implements Point {
// this class has an extra `name: string` property...
constructor (
public x: number,
public x: number,
public name: string
) {}
dist (other: NamedPoint) {
// other is a NamedPoint so other.name is okay, but this
// is not true of any Point, so how can NamedPoint be said
// to implement point?
if (other.name.startsWith()) { /* ... */ }
}
}
// this will throw if `a` is a NamedPoint and `b` is a GridPoint
function getDist (a: Point, b: point) {
console.log(`distance is: ${a.dist(b)}`)
}
// but tsc won't complain here:
getDist(new NamedPoint(1, 2, 'foo'), new GridPoint(9, 8));
link to full example on playground
Again, I'm certain that I'm phrasing this the wrong way in terms of "contravariance" but I would think that NamedPoint implements Point
would be forbidden by the compiler. I thought I could get this by turning on strictFunctionTypes
in tsconfig, but that apparently doesn't apply to this situation.
Is it my understanding of types that is incorrect, or is typescript wrong here? If it's the latter, can I do anything about it?