olly - stock.adobe.com

Devs discuss when to use TypeScript vs. JavaScript

TypeScript catches errors at compile time, which benefits developers and users. But the language isn't suitable for every project due to its additional overhead.

TypeScript, a superset of JavaScript, catches errors early in the development pipeline. But it comes with its own set of challenges, according to developer experts.

A debate about the merits of TypeScript vs JavaScript has been raging this month between developers online in the comments thread on a Hacker News question titled "Is TypeScript worth it?" That post, made Jan. 12, has touched off an intense debate, comprised of more than 400 comments to date, about the merits and shortcomings of TypeScript.

TypeScript adds a static type system to JavaScript, which means that scripts with errors will fail to compile. In addition to early bug detection, TypeScript's benefits include catching subtle integration issues that JavaScript might otherwise miss and more robust production code. But developers should weigh TypeScript's benefits against its overhead, which includes slowed-down development, when deciding when to use TypeScript vs. JavaScript, experts say.

Rather than having users report errors that happened at runtime, TypeScript gives developers an additional safety net to report errors at build time as part of its transpilation process, said Andrew Cornwall, an analyst at Forrester Research. Transpiling converts source code written in one language into another language with a similar level of abstraction.

Andrew Cornwall, senior analyst, Forrester ResearchAndrew Cornwall

"In other words, it shifts the discovery of the error left — which is almost universally considered a good thing," Cornwall said. "The earlier you can detect and correct an error, the cheaper it is to fix."

While cheaper fixes help to explain TypeScript's growing popularity, that shift-left safety net comes at a cost, Cornwall said. TypeScript is actively in development, so for developers, sometimes changes in the language will break existing code.

"This can be frustrating for developers," he said. "Nobody likes to have to change code that worked fine yesterday because the language itself changed today."

Language updates that break code aren't unique to TypeScript. For example, Apple's early versions of Swift introduced significant breaking changes. But things settled down as the language developers figured things out, Cornwall said.

"I expect TypeScript to follow a similar path," he said.

When to use TypeScript -- and when not to

Where TypeScript shines is when code gets complicated, particularly when a developer is using someone else's code. It will catch a subtle integration issue, such as "this value could be a string or a number," Cornwall said. One good reason to use TypeScript is when multiple developers collaborate on a project, he said.

"It will catch some API errors, where my assumption is different from my coworker's assumption," Cornwall said.

Rijk van Zanten, CTO, MonospaceRijk van Zanten

When it comes to writing production code, especially in a large open source project, such as Monospace's Directus data platform, strictly configured TypeScript has quickly become a requirement, said Rijk van Zanten, co-founder and CTO at Monospace, Inc., a data management platform company based in New York City.

"The enforced code style and strict type-checking means we have more trust in the code that we put out as a team," he said.

However, not every situation warrants the use of TypeScript. For example, if a developer is coding a single script on a web page, the overhead of adding TypeScript into the mix outweighs what it would save, Cornwall said.

"In other words, if my code is simple enough that I can test it thoroughly and don't need to integrate other packages with it, then I can write it, test it and get it off my plate more easily in plain JavaScript than in TypeScript," he said.

Van Zanten echoed Cornwall's thoughts on JavaScript's edge when it comes to speed for certain projects. For experiments, proof of concepts or otherwise 'private' code snippets, JavaScript often fits the bill.

"The lack of required build chain means you can iterate and test incredibly quickly," he said.

Nicolas Kokkalis, founder and head of technology of Pi NetworkNicolas Kokkalis

When a project involves many JavaScript developers with different skill sets, such as a large hackathon with thousands of JavaScript developers, it can be another reason to favor JavaScript, said Nicolas Kokkalis, founder and head of technology Pi Network, a cryptocurrency company in Palo Alto, Calif. While every TypeScript developer knows JavaScript, not all JavaScript developers are familiar with TypeScript, he said.

"You want to make your platform and your documentation as accessible as possible," he said.

Compatibility factors into TypeScript popularity

When developers are deciding when to use TypeScript vs. JavaScript, the two may not be mutually exclusive. Since TypeScript is a superset of JavaScript, in many cases, they can work hand in hand, Cornwall said.

Nobody likes to have to change code that worked fine yesterday because the language itself changed today.
Andrew CornwallAnalyst, Forrester Research

"I might have a large project that includes a lot of TypeScript but also a section that's pure JavaScript, because I know it works well and I don't want to touch it," Cornwall said. "Or it might do things like add properties to a JavaScript object on the fly. That's fine for JavaScript but not for TypeScript, which won't know whether my object has that property or not."

That compatibility is also a large factor in TypeScript's growing adoption rate, Cornwall said. In the 2021 StackOverflow survey, TypeScript was at 30.19% among professional developers for "Programming, Scripting and Markup Languages." By 2022 it was 40.08%.

"I'd expect more enterprises to adopt more TypeScript in the future," Cornwall said.

Microsoft, which developed and maintains TypeScript, declined to comment for this story.

Stephanie Glen is a writer, software developer and YouTuber based in Jacksonville, Fla. She can be reached at [email protected].

Dig Deeper on Software design and development