olly - stock.adobe.com
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.
"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.
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.
"The lack of required build chain means you can iterate and test incredibly quickly," he said.
"You want to make your platform and your documentation as accessible as possible," he said.
Compatibility factors into TypeScript popularity
Andrew CornwallAnalyst, Forrester Research
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].