From Type Compatibility - Introduction:
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.
From OOP paradigm perspective, below syntax is wrong, because Person
doesn't implements Named
let p: Named;
p = new Person();
Like C# & Java, C syntax also respects nominal typing
GO syntax respects structural typing.
In TypeScript language, Can structural type system and syntax using OOP paradigm co-exist?