Save over 40% when you secure your tickets today to TNW Conference 💥 Prices will increase on November 22 →

This article was published on August 18, 2021

Hey Python devs, don’t underestimate TypeScript

Consider switching if you’re dealing with front-end web or back-end Node development


Hey Python devs, don’t underestimate TypeScript

Python is my bread-and-butter, and I love it. Even though I’ve got some points of criticism against the language, I strongly recommend it for anybody starting out in data science. More experienced people in the field tend to be Python-evangelists anyway.

However, this doesn’t mean that you can’t challenge the limits in your field from time to time, for example by exploring a different programming paradigm or a new language.

TypeScript: JavaScript, but type-safe

About a decade ago, software engineers at Microsoft noticed that JavaScript wasn’t meeting all their needs anymore. On the one hand, the language was evolving rapidly and adding extremely interesting new features. On the other hand, none of the features in the pipeline could solve one fundamental problem: JavaScript was great for small programs, but writing whole applications with it was a mess.

There were a few possibilities for solving the problem: one could, for example, use a different language, or redesign JavaScript with a better syntax. The development team at Microsoft took a different approach: they created TypeScript by expanding JavaScript. This way, they were able to use all new features of JavaScript while getting rid of the stuff that wasn’t good for big projects.

Similar to Python, JavaScript is compiled at runtime, meaning that you need to run the code to debug it. TypeScript, on the other hand, is compiled. This provides an extra layer of safety because programmers get information about possible bugs before execution time. With non-compiled languages like Python or JavaScript, it can be quite time-consuming to locate bugs once you’ve realized that your code isn’t behaving as expected.

The key difference between JavaScript and TypeScript, however, is the type checker. Upon compilation, every TypeScript-program gets checked on whether the data types are consistent with one another.

This might sound trivial to do manually. But when you’re working on projects with thousands of lines of code, you’ll thank the Lord for having it.

There are a few other differences, like the fact that TypeScript has anonymous functions and asynchronous functions. Anonymous functions are a key feature of functional programming, which can make a program more efficient with big data loads.

Asynchronous programming is extremely useful when you need to perform multiple operations in parallel, or when you’re dealing with I/O operations that shouldn’t interrupt background processes. Asynchronous programming is possible in Python and Javascript, but in TypeScript it’s built in from the core.

TypeScript is a superset of JavaScript.

The list of Python’s competitors is long: Rust, Go, Scala, Haskell, Julia, Swift, C++, Java, and R all find an entry on it. In such a crowded field, it’s quite understandable that JavaScript and TypeScript don’t tend to get that much recognition.

That doesn’t mean that TypeScript isn’t a staple in the general programming world. Among data scientists, however, it’s never been particularly popular.

You could conclude that TypeScript may not be a good match for data science. But don’t rush. Although it might not be suitable for every part of data science, there are areas where it has distinct advantages over Python.

If you happen to work in one of these areas, it’s worth giving TypeScript a shot. And if you don’t, who knows where you’ll land next? The field is moving fast. You have a competitive advantage if you can look beyond your nose.

[Want to work at TNW? We’re looking for front-end developers to join our team]

How TypeScript became popular

If you hate the illogical but hilarious WTF-moments that keep happening in JavaScript, then I have bad news for you: you’ll see all that stuff in TypeScript, too.

But these syntactical hiccups aren’t the reason why programmers love TypeScript so much. Rather, it boils down to the idea that you take a great product — JavaScript — and add something even greater — static typing.

Of course, I’m not saying that JavaScript is always great, or that static typing is always the best way of doing things. But for building large Microsoft-style applications, this seems to work extremely well.

That being said, TypeScript still only has a fraction of the popularity that JavaScript has. This could be attributed to the age: TypeScript is eight years old. JavaScript is three times as old! And Python is also an oldie in the field with its thirty years.

Despite its young age, there are fields where TypeScript is inevitable. This adds to its popularity. For example, when Google announced that Angular.js would run with TypeScript in 2016, the number of tags on StackOverflow exploded.

Stack Overflow
Credit: Stack Overflow Trends
TypeScript only shares a fraction of the traction that Python and JavaScript have. Still, it’s inevitable in some areas

Where TypeScript might have an edge over Python

What made JavaScript popular back in the day is that it runs everywhere. You can run it on a server or in your browser or wherever you like. You compile it once, and it works everywhere. When that first came out, it almost seemed like magic.

As it’s built on JavaScript, TypeScript shares that magic. Python does, too! Granted, it has a slightly different implementation since it uses an interpreter instead of a virtual machine. But that doesn’t change the fact that in terms of run-me-anywhere, TypeScript and Python are on par.

Features like generics and static typing make it easier to do functional programming in TypeScript than in Python. This could be an advantage because demand for functional code is growing due to developments in data science, parallel programming, asynchronous programming, and more.

On the other hand, Python has been adding more and more features of functional programming, too. And when it comes to data science, machine learning, and more, Python is at the forefront of frontiers.

That leaves parallel programming and asynchronous programming on the table. Even though you can pull both of these things off in both languages, there is a big difference: in Python, you need to use particular libraries for the task. In TypeScript, all libraries are asynchronous from the core. And since the latter is a bit more functional by default, it’s often a tiny bit easier to do parallel programming.

In other words, if you’re a Python developer that is involved in asynchronous processes and parallel computing, you might want to give TypeScript a try.

What makes TypeScript great for data science — or not

Many data scientists deal with asynchronous and parallel programming. You might already be considering writing your next project in TypeScript rather than Python. Whether that’s a good idea depends on many other factors, though.

First of all, TypeScript doesn’t have a straightforward way of doing list comprehensions. This can be frustrating when dealing with large arrays, for example.

Second, there are no native matrix operations in TypeScript. Python has NumPy, as well as a host of other tools, that make them easy. So if your project is heavy in linear algebra, you might want to stay away from TypeScript.

Third, if you’re not too familiar with JavaScript, you’re almost guaranteed to get some moments of confusion. Since TypeScript is built upon JavaScript, it inherited all of its features — the good, the bad, and the WTF. Although, if I’m honest, encountering these phenomena can be quite amusing, too…

Finally, you’ll want to take into account that programming isn’t a solitary occupation. There is an enormous community for Python in data science that offers support and advice. But at this point in time, TypeScript isn’t that popular among data scientists. So you might not be able to find that many helpful answers to your questions on StackOverflow and elsewhere.

That being said, if you’re starting a small project without too many big arrays and matrix operations, you might want to give TypeScript a go anyway. Especially if it involves some parallel or asynchronous programming.

The bottom line: know where to use your tools

There is no one language for every task. Sure, some languages are more fun or more intuitive than others. Of course, it’s important that you love your tools because that will keep you going when times are tough. Starting with a well-loved language like TypeScript or Python is therefore not a bad idea.

But at the end of the day, you shouldn’t stick to one language like to a religion. Programming languages are tools. Which tool is best for you depends on what you’re trying to do with it.

At the moment, Python is huge for data science. But in a rapidly evolving field, you need to be able to look past your nose. As your tasks are changing, so might your tools.

TypeScript, on the other hand, has a buzzing community around front-end web, back-end Node, and mobile development. What’s interesting is that these areas intersect with data science more often than one thinks. Node, in particular, is gaining more and more traction among data scientists.

Of course, this doesn’t mean that you should dabble with a dozen languages at a time. There is enormous value in knowing one language really well. But being curious about other languages and technologies will help you stay ahead of the curve in the long term.

So don’t hesitate to try something new when you feel like it. Why not with TypeScript?

This article was written by Ari Joury and was originally published on Towards Data Science. You can read it here.

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with