TypeScript
  • Introduction
  • Introduction
    • What is TypeScript?
    • Why TypeScript?
    • Setup
  • Beginner
    • Basic types
      • Primitive types
      • Arrays and tuples
      • Enums
      • Any type
    • Objects
    • Type aliases
    • Interfaces
    • Functions
      • Function signatures
      • Void type
      • Functions as types
    • Union types
    • Type guards
    • Intersection types
    • Nullable types
    • Inference
  • Resources
    • Resources
Powered by GitBook
On this page
  • Static typing benefits
  • Type definitions
  • Developer happiness 👩‍💻
  • TypeScript vs FlowType?
  1. Introduction

Why TypeScript?

PreviousWhat is TypeScript?NextSetup

Last updated 6 years ago

As we saw in , there are two main goals of TypeScript:

  1. Add an optional type system to JavaScript syntax

  2. 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.

Static typing benefits

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.

Type definitions

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

npm install --save-dev @types/foo

...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 .

Developer happiness 👩‍💻

TypeScript vs FlowType?

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:

What is TypeScript?
intelligent code completion
DefinitelyTyped
TypeSearch
third most-loved programming technology
https://slack.engineering/typescript-at-slack-a81307fa288d
https://eng.lyft.com/typescript-at-lyft-64f0702346ea
https://engineering.tumblr.com/post/165261504692/flow-and-typescript
https://redditblog.com/2017/06/30/why-we-chose-typescript/