When building applications nowadays, we can feel flattered by the variety of tools and concepts that aim to help us ship more reliable code to production. There are many ways for us to become more confident about the code we write.
Among the options for shipping reliable code is static typing, which is a good way to catch errors early in the development cycle. Static typing allows for a faster development process while helping check the correctness of our code. In this article, we’ll talk about two languages that offer static typing: Dart and TypeScript.
- Why static type checking?
- General features of Dart and TypeScript
- Comparing Dart and TypeScript by category
Why static type checking?
Although the focus of this article is not to define static typing, it is important to revisit the reason why writing statically typed code can be a great benefit to the development experience.
Static typing allows us to analyze variable types at compile time, meaning that before executing your code, you will be able to know if there are errors with the way you’re using variables, like what types of values you’re assigning to them, for instance.
This helps decrease the amount of time spent debugging errors that might happen during runtime simply because, for example, you are calling a string related method on a variable that holds an integer value at some point in your code.
Because nothing comes without sacrifice, we should take into consideration the extra work we have to do when defining functions and variables that have explicit types. That work can vary from a few extra keystrokes to defining an entire object structure, specifying types for its keys and values while thinking about how to organize those types for reusability.
General features of Dart and TypeScript
Among statically typed languages, Dart and TypeScript are well-known and well-established options, each with distinct features. The goal of this article isn’t to take sides but to review the features of each language and how they compare to each other. Hopefully, this will give you useful insights on when and how to use them.
Comparing Dart and TypeScript by category
For easier comparison, let’s select a few main points in which the two languages show differences and similarities.
Both Dart and TypeScript are considered multi-paradigm languages, meaning they offer features that allow the usage of techniques included in more than one programming paradigm.
For example, you can use either Dart or TypeScript when following a functional paradigm, where a program is built around functions that should handle immutable data and avoid sharing state. This is also true when you want to work with an object-oriented paradigm because both of them offer features included in classical OOP programming, such as classes, inheritance, encapsulation, and polymorphism.
Developers might have a preference for either Dart or TypeScript because there are a few differences in the way such features are implemented in each. One example of something that is not offered in Dart is method overloading, a useful technique where a single method name is used for multiple methods that implement different behaviors based on the method’s input.
Trying to achieve method overloading with Dart will require you to implement workarounds that might not always look great. Meanwhile, TypeScript officially supports the technique, which is implemented with its own specificities and limitations, like not being able to have different bodies for each method definition, forcing you to handle the different input types within one scope.
Before choosing Dart or TypeScript, you might want to not only do research on which paradigm you plan on following for your code, but also on how specific features or patterns are offered within each language.
Dart and TypeScript syntax
Below are examples of the same program in Dart and Typescript, respectively. Both scripts take one random and one hardcode integer, multiply them, and print the result:
GitHub communities and surrounding tools
As mentioned, both languages are open source and offer a welcoming environment for contributions. This is something to keep in mind and track because it tells you how willing developers will be to help improve the language and build tools surrounding it.
As for TypeScript’s GitHub repository, as of the time of writing, it has 84.7K stars. For this comparison, the total number of npm packages with type declarations for libraries hosted on npm is 8,803. This is, of course, not including all other TypesScript-related packages that are not prefixed by “@types/” (the term used for the search in npm).
Dart and TypeScript uses and applications
As previously mentioned, Dart is a general-purpose language, meaning that even with its optimization surrounding client-focused projects, it is also used on the server side, with applications on command-line apps and IoT projects. Here is an example.
That is possible due to the multi-platform characteristic of Dart, which compiles optimized code to different platforms:
Choosing Dart or TypeScript for your next project is a matter of weighing the factors you are dealing with and the goals for your project. Both of them will offer you features for developing reliable large-scale applications. But time, developer experience, community, and the type of application you’re developing are all variables that will help you choose one or the other.
LogRocket: Full visibility into your web and mobile apps
LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.