Seiten

Typescript duck typing

One of the main surprising feature of TypeScript in my opinion is ths so called "duck typing" that works great for interfaces in Typescript. "duck typing" is a concept for dynamic typing that has been integrated in Ruby and means:
"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck." (by James Whitcomb Rileys)
 How does it work for interface? An Example:

Lets have the following object declarations:

let myObj = {

     lastname: "Petersen",
     surname: "Peter"
}

let myObj2 = {

     fullname: "Petersen",
     titel: "Dr."

and a simple interface declaration:

interface Name {

     lastname: string,
     surname: string
}

then the following expression is vaild in TypeScript:

let name: Name = myObj; 

but NOT this expression:

let name2: Name = myObj2;

Ok, this may be not be surprising at a second glance, but for a developer that has been shaped and stigmatized by Java for many years it is a surprisingly simple but effective feature.

Angular (2) module and ES6 modules

Diving deeper into Angular (2) during the last weeks I came to attention about the difference of the Angular2 module (NgModule) concept in comparison with the ES6 module concept:


ES6 modules export JavaScript elements per file. This is a low level module concept on basis of the programming language without any semantic meaning.

Angular modules define modules in the semantics of Angular as a framework for building single page web/mobile apps. An Angular module defines which Angular specific elements
  • belong together, 
  • are exported and 
  • are imported.
Further Angular specific elements are Components, Directives, Filters and Services. Such semantic could not be achieved with the use of ES6 modules. The Angular module concept is completly disjoint from ES6 modules as Angular does not make any requirements about the organization of Angular2 elements in files (although this topic is covered in the Angular2 styleguide which should be take into account in any case).

Some may think that the above comparsion is superfluous because the difference is quite clear, but I found some so called 'professional' articles that lack clear statements, like the following I found describing Angular modules in short:

"Modules are blocks of code that do a certain type of task. A module exports some value in the main code, such as class. The first and the most common module that you will study is the one that exports Component Class."

This explains ES6 modules that are part of the language but not the Angular module concept in any way.

Update, 03/01/2017:
Here are some sources about the topics:
Javascript and Modules
Angular Modules

JavaScript Reuse by Inheritance

After having studied some JavaScript books I discovered some useful parts of the JavaScript language that best suited to me. One of my good parts is concerning patterns for reuse by inheritance:

Avoid class like inheritance using the new function. Especially for Java developers this approach might cause confusion and is error prone because inheritance using prototypes does not always behaves like some class minded developer might think (e.g. overloading of properties, prototype vs. local properties/methods). As inheritance is mainly used for the reuse of application logic, JavaScript offers other concepts for reuse that fit better with the dynamic aspect of the language:

  • Copying of properties/methods,
  • Mix-Ins or 
  • Borrowing Methods
(as mentioned in JavaScript Patterns by Stefanov).