Fun with Stamps. Episode 16. TypeScript mix-in classes vs Stamps

Vasyl Boroviak
Jul 21, 2017 · 4 min read

Hello. I’m developer Vasyl Boroviak and welcome to the sixteenth episode of Vasyl Boroviak presents Fun with Stamps.


TL;DR

In the bottom you will find my fantasy syntax of stamps if it was part of the TypeScript.

What are TypeScript mix-in classes?

Recently, the TypeScript v2.2 released a cool new feature — mix-in classes. Basically, you can “mix classes” now. This is exactly the purpose of stamps — “mix stamps”.

TypeScript is a great language. It gives you tight control over your object types, which is often a beneficial feature.

In this episode the only thing I would like to point out is that the “mix-in classes” feature is hard to read. Of course it depends on your TypeScript skills. But let’s imagine you are a mediocre lazy developer who’s reading the code first time ever.

Here is the official example from TypeScript v2.2 release notes.

How much time does it take you to understand what’s going on here?

Let’s rewrite it using stamps.

How much time you, my imaginary mediocre developer, need to understand the code below?

In stamps you can provide all the arguments to the initializer (aka constructor). In TypeScript you have to assign the properties after you created an object.

But most important, which is more readable to you?

Fantasy syntax of TypeScript

TypeScript has a whole bunch of other amazing features. But I wish stamps where part of TypeScript rather than a separate module.

Example syntax:

That’s a good start! Let’s fantasise more. Let’s convert the simple stamps from the very first episode of Fun with Stamps. Properties, methods, initializers:

How about statics?

It would be awesome if developers could choose between the stamp() and extends syntax at will. Just like we can choose between the .then() and await syntax in ES6.

The other bits of TypeScript should work with stamps same as they work with classes (generics, interfaces, etc).

That is very appealing. This syntax would convince me to go TypeScript full time.

Differences between TypeScript classes and Stamps

  1. With stamps the syntax is becoming simpler and shorter.
  2. With stamps you can have as many constructors as you wish. All will receive exactly the same arguments.
  3. In stamps there is no inheritance chain, — that’s the main reason why syntax is much simpler. All the methods do go to the __proto__ of your object instances for performance reasons.
  4. You can do multiple inheritance.
  5. Stamps do not require the new keyword. Thus, can be replaced with a simple factory function if needed.
  6. With stamps you have direct access to your stamp’s meta-data. See here. You can build smarter things having that information.
  7. The instanceof won’t work with stamps by design. (If really needed I’d recommend implementing TypeScript obj is Foo keyword as obj.__proto__ === Foo.stamp.methods. But better not to have it at all.)
  8. … I was trying hard to find downsides of the stamp approach, but failed. Please, help me …

Have fun with stamps!


)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade