Why Learn TypeScript

By Zain Ali

"Explore why TypeScript’s type safety and scalability make it a powerful choice over JavaScript."

October 3, 2024

Why Learn TypeScript

Why Learn TypeScript: A Beginner's Guide to TypeScript Features and Comparison with JavaScript

TypeScript has rapidly gained popularity as an alternative to JavaScript due to its added features, especially for large-scale applications. Created by Microsoft, TypeScript offers a statically typed experience, enabling developers to catch errors earlier, write more predictable code, and scale applications smoothly. In this post, we’ll explore the main reasons to choose TypeScript over JavaScript, highlight key TypeScript features, and showcase side-by-side code comparisons.


1. What is TypeScript?

TypeScript is a superset of JavaScript that adds optional static typing. Unlike JavaScript, TypeScript lets you define types for variables, function parameters, and return values, providing an extra layer of safety and predictability in your code. TypeScript code compiles to plain JavaScript, making it compatible with all JavaScript environments.


2. Why Choose TypeScript Over JavaScript?

While JavaScript is flexible and forgiving, this can also lead to hidden errors and unpredictable behaviors in larger codebases. Here’s how TypeScript addresses these challenges:

  • Static Typing: With TypeScript, you define types, enabling the compiler to catch errors at build time instead of at runtime.
  • Improved Code Quality and Predictability: Types act as documentation, making code more readable and reducing ambiguity.
  • Better Tooling and Autocompletion: IDEs like VSCode provide enhanced autocompletion and error-checking with TypeScript.
  • Easier Refactoring: As your project grows, TypeScript makes refactoring easier by providing clear, type-safe guidance.
  • Scalability: For large projects with multiple developers, TypeScript’s structure reduces bugs and ensures consistency across the codebase.

3. Key Features of TypeScript

  • Static Typing: Define specific types for variables, making it easier to catch type-related errors during development.
  • Interfaces: Allows you to define the shape of an object, enforcing structure without implementation.
  • Enums: Offers a way to define named constants, which can improve readability and reduce hard-coded values.
  • Generics: Type-safe functions or classes that can operate on various data types, enabling reusability.
  • Access Modifiers: Control access to class members with public, private, and protected.

4. Code Comparison: JavaScript vs. TypeScript

Let's look at some key differences through practical code examples.

Example 1: Variable Typing

JavaScript

let message = "Hello, world!";
message = 42; // No error, even though it changes from string to number

TypeScript

let message: string = "Hello, world!";
// message = 42; // Error: Type 'number' is not assignable to type 'string'

Comparison: In JavaScript, variables are dynamically typed, so a variable can hold any type of value, which can lead to errors. TypeScript requires you to declare the type, preventing unintended type changes.


Example 2: Function Parameter and Return Types

JavaScript

function greet(name) {
  return "Hello, " + name;
}
 
console.log(greet(42)); // Outputs: Hello, 42

TypeScript

function greet(name: string): string {
  return "Hello, " + name;
}
 
// console.log(greet(42)); // Error: Argument of type 'number' is not assignable to parameter of type 'string'

Comparison: TypeScript enforces parameter and return types, preventing runtime errors from unexpected data types, as seen above when trying to greet a number.


Example 3: Interfaces

TypeScript's interfaces allow you to define the structure of an object, making it clear and maintainable.

JavaScript

const person = { name: "Alice", age: 30 };
 
function displayPerson(person) {
  console.log(person.name);
}
displayPerson(person);

TypeScript

interface Person {
  name: string;
  age: number;
}
 
const person: Person = { name: "Alice", age: 30 };
 
function displayPerson(person: Person): void {
  console.log(person.name);
}
displayPerson(person);

Comparison: In TypeScript, the Person interface defines the expected structure, helping catch errors if the object doesn’t match the specified shape.


Example 4: Enum Types

Enums in TypeScript allow you to define a set of named constants, adding readability and preventing magic numbers or strings.

JavaScript

const RED = "RED";
const GREEN = "GREEN";
const BLUE = "BLUE";
 
function getColor(color) {
  console.log(color);
}
getColor(RED);

TypeScript

enum Color {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}
 
function getColor(color: Color): void {
  console.log(color);
}
getColor(Color.Red);

Comparison: TypeScript enums offer a safer, cleaner way to handle constants, reducing the risk of typos and making code more readable.


Example 5: Using Generics

Generics allow you to create functions or classes that can work with multiple types, providing flexibility with type safety.

JavaScript

function identity(value) {
  return value;
}
 
console.log(identity(5));       // Works
console.log(identity("Hello")); // Works

TypeScript

function identity<T>(value: T): T {
  return value;
}
 
console.log(identity<number>(5));         // Works
console.log(identity<string>("Hello"));    // Works

Comparison: Generics in TypeScript ensure that the function works with the specified type, providing clearer code and preventing type-related issues.


5. TypeScript vs. JavaScript: Key Differences


Feature: Typing

  • JavaScript: Dynamic
  • TypeScript: Static (optional)

Feature: Compilation

  • JavaScript: Interpreted
  • TypeScript: Compiled to JavaScript

Feature: Interfaces

  • JavaScript: Not supported
  • TypeScript: Supported for defining object structure

Feature: Generics

  • JavaScript: Not supported
  • TypeScript: Supported, enabling reusable and type-safe functions or classes

Feature: Tooling

  • JavaScript: Basic autocompletion
  • TypeScript: Enhanced autocompletion and error detection

Feature: Error Detection

  • JavaScript: Errors found at runtime
  • TypeScript: Errors detected at compile-time

Feature: Enums

  • JavaScript: Not available
  • TypeScript: Available, allowing named constants

6. Is TypeScript Worth It?

For small projects or simple scripts, JavaScript’s flexibility may be sufficient. However, if you’re working on a large application, TypeScript's type-safety, enhanced tooling, and compile-time error detection can save time and improve code quality, especially for teams with multiple developers.

  • Pros of TypeScript: Early error detection, better tooling, improved readability, scalable codebase.
  • Cons of TypeScript: Slightly steeper learning curve, additional setup, and may feel restrictive for rapid prototyping.

7. How to Get Started with TypeScript

To start using TypeScript in a project:

  1. Install TypeScript: npm install -g typescript
  2. Initialize a TypeScript Project: tsc --init (Creates tsconfig.json file)
  3. Compile TypeScript to JavaScript: Run tsc in the terminal to compile .ts files into .js.

Example:

// example.ts
const add = (a: number, b: number): number => {
  return a + b;
};

Compile with:

tsc example.ts

The above command generates example.js file with plain JavaScript code.


Conclusion

TypeScript’s structured approach can bring many benefits over JavaScript, from improving readability and maintainability to catching errors early in the development process. Whether you’re a beginner or an experienced developer, learning TypeScript can help you write safer, more scalable code, ultimately boosting productivity. By taking advantage of TypeScript’s features—such as static typing, interfaces, and enums—you can unlock a more robust way to write JavaScript applications.

Happy coding in TypeScript!