Why TypeScript?
Last updated
Last updated
As we saw in , there are two main goals of TypeScript:
Add an optional type system to JavaScript syntax
Allow developers to use the latest JS features (ES.Next) before waiting for browser adoption
Unlike JavaScript, which is dynamically typed, in TypeScript every value is of a certain type, which must be known at compile time. This makes TypeScript a statically typed language. Types are essentially metadata which tell the compiler what kind of data is being specified so it knows how to work with that data.
When viewed the context of Shopify's organizational scale and structure, the benefits granted by using a statically typed language are compounded. Let's look at a few of the benefits of having static types:
Finding errors at compile time is better than finding errors at run time.
Types are a good form of documentation. As codebases grow, so does the importance of self-documenting code.
Types can inform external tools such as and documentation generators.
Types provide greater confidence when refactoring. For example, in a static language, updating the declared type signature of a function will cause compile time errors in all the places that need updating.
is a community-driven resource that provides high quality TypeScript type definitions. In practice, this means you can add types to any existing JavaScript library, and reap the rewards immediately.
Simply pull the type definitions into your project with the command
...and you're set!
For an NPM package foo
, typings for it will be at @types/foo
. If you can't find your package, look for it on .
A lot of developers may ask, why did we chose TypeScript instead of Flow? While both are great, we at Shopify opted for TypeScript over Flow for the following reasons:
TypeScript has better community typing (see DefinitelyTyped)
TypeScript offers better editor integration (in part due to a larger ecosystem, but also, VSCode is itself written in TypeScript, and thus integrates extremely well)
.ts/.tsx
extension. Having type annotation in a .ts
file is honest; whereas flow encourages you to have .js
extension, which could result in a lot of pain when considering upgrade paths and deprecation paths.
Additionally, these other organizations have evaluated both options, ultimately siding with TypeScript. While their rationale may differ from ours at Shopify, it's interesting to see what additional benefits they found.
In the 2017 StackOverflow Developer Survey, TypeScript was the .
Slack:
Lyft:
Tumblr:
Reddit: