By Yakov Fain | Article Rating: |
|
July 11, 2018 08:29 AM EDT |

After completing the second edition of the book “Angular Development with TypeScript“, my colleague Anton Moiseev and I started working on yet another book for Manning. This one will be called “Get Programming with TypeScript” and its tentative Table of Content is available here.
This book will cover the main syntax elements of the TypeScript language, and to make the book more interesting, we’ll also develop a blockchain app.
Meanwhile, I’ll start a series of blogs on the TypeScript-related topics. This one is about TypeScript’s structural type system.
A primitive type has just a name (e.g. number) while a more complex type like an object or class has a name and some structure represented by properties (e.g. a class Customer has properties name and address).
How would you know if two types are the same or not? In some languages (e.g. Java) two types are the same if they have the same names, which represents a nominal type system. In Java, the last wouldn’t compile because the names of the classes are not the same even though they have the same structure:
class Person { String name; } class Customer { String name; } Customer cust = new Person(); // compiler's error
But TypeScript and some other languages use the structural type system. In the following code snippet I re-wrote the above code snippet in TypeScript:
class Person { name: string; } class Customer { name: string; } const cust: Customer = new Person(); // no errors
This code doesn’t report any errors because TypeScript uses structural type systems, and since both classes Person and Customer have the same structure, it’s OK to assign an instance of one class to a variable of another.
Moreover, you can use object literals to create objects and assign them to class-typed variables or constants as long as the shape of the object literal is the same. The following code snippet will compile without errors:
class Person { name: string; } class Customer { name: string; } const cust: Customer = { name: 'Mary' }; const pers: Person = { name: 'John' };
Our classes didn’t define any methods, but if both of them would define a method(s) that has the same signature (name, arguments, and the return type) they would also be compatible.
What if the structure of Person and Customer are not exactly the same? Let’s add a property age to the class Person as is the following listing:
class Person { name: string; age: number; // 1 } class Customer { name: string; } const cust: Customer = new Person(); // still no errors
1 We’ve added this property
Still no errors! TypeScript sees that Person and Customer have the same shape. We want to use the constant of type Customer (it has the property name) to point at the object of type Person (it also has the property name).
Follows the link https://bit.ly/2MbHvpH and you’ll see this code in TypeScript playground (a REPL to try code snippets in TypeScript and compile them into JavaScript). Click Ctrl-Space after the dot in cust. and you’ll see that only the name property is available even though the class Person has also the property age.
Homework: Can the class Customer have more properties than Person?
In the previous code snippet, the class Person had more properties than Customer and the code compiled without errors. What if the class Customer has more properties than Person? Would the following code compile? Explain your answer.
class Person { name: string; } class Customer { name: string; age: number; } const cust: Customer = new Person();
Read the original blog entry...
Published July 11, 2018
Copyright © 2018 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Yakov Fain
Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain
- Secrets Of The Masters: Core Java Job Interview Questions
- A Cup of AJAX? Nay, Just Regular Java Please
- Rich Internet Applications with Adobe Flex 2 and Java
- Teaching Kids Programming: Even Younger Kids Can Learn Java
- Reading Data from the Internet
- Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)
- Java Basics: Introduction to Java Threads, Part 1
- Java Serialization
- Are You Using Abstract Classes, Polymorphism, and Interfaces?
- SYS-CON Webcast: Eclipse IDE for Students, Useful Eclipse Tips & Tricks