While JavaScript is frequently the
language of choice for all sizes of frontend and backend applications, it's not
the only option. Nor is it necessarily the most efficient or cost-effective.
Increasingly, TypeScript is becoming the go-to language for app development ─
particularly for larger apps. The time- and cost-saving benefits are
significant enough that some organizations are even taking projects initially
started in JavaScript and migrating them to TypeScript.
What is TypeScript?
While the number of programs written
in JavaScript has grown exponentially, the programming language's ability to
express the relationships between different units of code and mitigate coding
errors early on hasn't kept pace. Along with JavaScript's inconsistent
semantics, this makes JavaScript-driven app development difficult to manage at
scale.
Released in 2012, TypeScript was
created to address JavaScript's deficiencies in developing large-scale apps.
It's an open source, strongly typed programming language that builds on
JavaScript by adding optional static typing. Types enable structuring and
validating code before it's executed, which is beneficial in developing large
apps. They also provide additional information about code, which serves as
better documentation for other developers and facilitates collaboration.
TypeScript is a superset of
JavaScript, which means any JS code is also valid TS code ─ provided the TS
configuration is set to be compatible with it. It outputs code in pure
JavaScript and allows developers to liberally use JS libraries, tools, and
frameworks. It runs on Node.js or any browser that supports ECMAScript 3 or
higher. It also supports object-oriented programming features.
JavaScript Drawbacks
JavaScript is one of the world's
most popular programming languages and works extremely well for small projects.
However, its disadvantages ─ many of which affect projects costs and code
delivery time ─ make a strong case for considering alternatives such as
TypeScript, particularly for larger projects.
Much of it has to do with the lack
of types and compile-time error checks, making JavaScript less than desirable
for server-side code in enterprises and large codebases. The following are just
a few of its disadvantages:
- JavaScript employs dynamic typing, which means scripts
can compile even if they contain errors that can prevent them from
properly running.
- Because JavaScript is dynamically typed, it allows one
variable to have multiple properties assigned to it. It doesn't instantly
let developers know what a variable can contain. That makes it easy to
assign the wrong properties.
- Since JavaScript is an interpreted language, errors can
only be found during run-time. Code needs to run first to be tested and
validated, so it can take considerable time to find bugs and errors in the
code.
- To eliminate inaccuracies, it's necessary to manually
verify types and the syntactic correctness of the code. This lengthens
development time and extends the delivery cycle to production, increasing
development costs.
- JavaScript code is executed on the client-side, so it's
viewable to the user. As a result, bugs and oversights have the potential
to be exploited for malicious purposes.
- Lengthy onboarding of new developers can result because
developers must figure out the properties of the structures they're
working with, as well as the data types. While JSDoc can be used for
documenting code and annotating types, there's still the need to
synchronize the actual code and documentation. The lack of
synchronization, in turn, can mislead developers and complicate the
introduction of new, business-requested features.
- JavaScript is prototype-based, not class-based. It's
not considered a pure object-oriented programming language, although it
can follow some object-oriented programming principles.
The TypeScript Advantage
TypeScript is a superset of
JavaScript. If a developer knows JavaScript, there's not much of a learning
curve to take advantage of the features TypeScript offers that make up for many
JavaScript's deficiencies ─ and offer additional benefits.
For example, with JavaScript,
variables can start as one property, and then change into an object or a
string. These inconsistencies can generate problems that are difficult to
resolve in large apps. TypeScript, on the other hand, analyzes the code and
tries to determine proper variable types prior to runtime. Once a variable type
is assigned, it stays unchanged. TypeScript's compiler also helps shorten the
QA and testing process in later stages of development.
TypeScript also helps developers
quickly figure out the purpose of a variable within the code. It can also
suggest available properties in functions, classes, or components. Being able
to quickly lookup a variable is important because it reduces the likelihood of
calling the wrong function or accidentally skipping a variable declaration. Any
reduction in bugs and errors reduces the time required for fixing those issues
and in overall development time. That gives developers more time to work on app
logic and fix errors that can be detrimental to in-app performance and
usability. According to a postmortem analysis by Airbnb, 38% of bugs were preventable with TypeScript after the
company adopted it throughout the organization.
As a static language, TypeScript
performs type checks upon compilation, flagging type errors and helping
developers spot mistakes early on in development. Reducing errors when working
with large codebases can save hours of development time.
Clear and readable code is easy to
maintain, even for newly onboarded developers. Because TypeScript calls for
assigning types, the code instantly becomes easier to work with and understand.
In essence, TypeScript code is self-documenting, allowing distributed teams to
work much more efficiently. They don't have to spend inordinate amounts of time
familiarizing themselves with a project.
TypeScript's integration with
editors also makes it much easier to validate the code thanks to context-aware
suggestions. TypeScript can determine what methods and properties can be
assigned to specific objects, and these suggestions tend to increase developer
productivity.
TypeScript is widely used to
automate the deployment of infrastructure and CICD pipelines for backend and
web applications. Moreover, the client part (for example, when using Angular)
and the backend can be written in the same language - TypeScript. This
flexibility allows an engineer who knows one programming language to cover all
parts of the system.
Because TypeScript essentially
transpiles to JavaScript, migrating existing code to TypeScript is easy and
fast. It can typically be accomplished simply by running the compiler and
adding typing where it's not recognized by the language. There's no need to
make changes to the code.
The benefits of TypeScript have been
noticed by developers. TypeScript was used by 78% of the 2020 State of JS
respondents, with 93% saying they would use it again. Typescript continues to
grow in popularity. It was voted the second most loved programming language in
the Stack Overflow 2020 Developer survey.
TypeScript & AWS: Better Together
There's another compelling reason to
consider TypeScript for large-scale app development projects. It's fully
supported by AWS, the leading cloud platform for modern app design and
development as noted in Gartner's Magic Quadrant for Cloud Infrastructure and
Platform Services.
The following services and tools
have seamless integration out-of-the-box with TypeScript, enabling you to use
this language for a vast number of tasks:
- AWS
CDK provides Infrastructure as
code (IaC) to deploy the entire infrastructure in one click in an
automatic mode. You can also automate the creation of CI/CD pipelines for
the future launch of specific jobs on demand.
- AWS
Lambda allows you to run computation
tasks in serverless mode using automatic scaling and an efficient pricing
scheme.
- Amazon EC2, ECS, and EKS can cover any of your tasks and provide a wide range
of solutions for running apps written in TypeScript - from bare metal
servers to complex clusters of hundreds of Docker containers.
Optimize Your Application Development Projects
All decisions made during an application
development project can impact overall costs and time to market. That includes
going with the right programming language and using the most appropriate cloud
platform and resources.
When approaching an app dev project,
a one-size-fits-all approach rarely works. Take the time to define your needs
and priorities and select the best resources to deliver the best results.
##
ABOUT THE AUTHOR
Vyacheslav Gorlov
is the Head of Data Science at ClearScale.