Da Compatibilità dei tipi - Introduzione :
Structural typing is a way of relating types based solely on their members. This is in contrast with nominal typing. Consider the following code:
interface Named { name: string; } class Person { name: string; } let p: Named; // OK, because of structural typing p = new Person();
In nominally-typed languages like C# or Java, the equivalent code would be an error because the
Person
class does not explicitly describe itself as being an implementor of theNamed
interface.TypeScript’s structural type system was designed based on how JavaScript code is typically written. Because JavaScript widely uses anonymous objects like function expressions and object literals, it’s much more natural to represent the kinds of relationships found in JavaScript libraries with a structural type system instead of a nominal one.
Dal punto di vista del paradigma OOP, la sintassi di sotto è sbagliata, perché Person
non implements Named
let p: Named;
p = new Person();
Come C # & La sintassi di Java, C rispetta anche tipizzazione nominale
GO rispetta la sintassi tipizzazione strutturale .
Nel linguaggio TypeScript, Can sistema di tipo strutturale e la sintassi utilizzando paradigma OOP coesistono?