I'm studying Typescript reading Handbooks & Docs. And i'm just curious about how Typescript Duck Typing Works.
For example, this code provides Type error (using Typescript official Playground)
interface Point {
x: number;
y: number;
}
const p: Point = {
x: 1,
y: 2,
z: 3
}
function printPoint(p: Point) {
console.log(p.x + p.y);
}
printPoint(p);
but this code does not provides Type error
interface Point {
x: number;
y: number;
}
const p = {
x: 1,
y: 2,
z: 3
}
function printPoint(p: Point) {
console.log(p.x + p.y);
}
printPoint(p);
i thinks the problem is related to Typescript's Duck Typing, but i could not understand why argument's type system & constant's type system works in different way.
did i misunderstand about the Duck Typing? const p contains x, y, so i think const p: Point should work