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 the Named 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?

  • 5
    What precisely is this "OOP paradigm syntax"? Person does implement the Named interface, it just doesn't explicitly say so in its definition. – jonrsharpe Nov 7 '17 at 7:29

Your assertion that

let p: Named;
p = new Person();

is wrong from an OOP paradigm perspective is incorrect.

The OOP paradigm does not require that the relations between types are expressed explicitly in the type system of the language you are using. This means that a structural type system, where relations between types are implicit can very well be used together with the OOP paradigm.

  • From OOP paradigm perspective, relation between types Named and Person should either be is-a or implements, for the syntax let p: Named;p = new Person(); to be valid. – user1787812 Nov 7 '17 at 8:14
  • 4
    @user1787812 No. Let go of your Java/C# preconceptions. – Sebastian Redl Nov 7 '17 at 8:18
  • 1
    @user1787812 From OOP paradigm perspective you do not even need a type system in the language, let alone static types. – Goyo Nov 7 '17 at 8:51
  • @SebastianRedl What does No mean? Can you elaborate? – user1787812 Nov 7 '17 at 13:06
  • 2
    @user1787812 It means your comment is just wrong. From an OOP paradigm perspective, there doesn't even need to be such a thing as a "type", much less any specific relations between them. A unityped language is perfectly capable of being OO, e.g. by following the prototype object paradigm. – Sebastian Redl Nov 7 '17 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.