TypeScript’s type system checks compatibility based on the structure of the data, not the explicit type name.
interface Point {
x: number;
y: number;
}
function logPoint(p: Point): void {
console.log(`${p.x}, ${p.y}`);
}
const point1 = { x: 12, y: 26 };
logPoint(point1); // Output: 12, 26
// Extra properties are fine when not explicitly checked
const point2 = { x: 12, y: 26, z: 89 };
logPoint(point2); // Output: 12, 26
class VirtualPoint {
constructor(public x: number, public y: number) {}
}
const vp = new VirtualPoint(13, 56);
logPoint(vp); // Output: 13, 56