As mentioned in TypeScript handbook:
One of TypeScript’s core principles is that type-checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”.In TypeScript, interfaces fill the role of naming these types,...
My understanding is, the above core principle does not relate to Duck typing but Structural typing, because TypeScript is static typed language.
As mentioned in wiki: It requires that type checking be deferred to runtime, and is implemented by means of dynamic typing or reflection... an object's suitability is determined by the presence of certain methods and properties (with appropriate meaning), rather than the actual type of the object.
How do I understand the above core principle of TypeScript?
This is sometimes called "duck typing"
... I don't see a claim that TypeScript is formally a Duck Typing system, per the strict definition, and a quick google search shows this statement to be true. TypeScript doesn't really fit into the wikipedia article because it's not a runtime language at all, it's just a type-checker. However it certainly meets several of the criteria, including the basic duck principal and the fact it compiles to JavaScript, a dynamically typed language. – Jaffalet myObj:labelledValue = {size: 10, label: "Size 10 Object"};
has to go for compile-time type check, which follows structural typing. 2) Parameter object received infunction printLabel(labelledObj: LabelledValue) {..}
can only be checked at runtime, where there is no type(function printLabel(labelledObj) {}
), so obviously it is the same rules that JS follows(duck typing), in second case. – Anastassialet myObj = {size: 10, label_1: "Size 10 Object"};
. Code still works dues to duck typing. – Anastassia