DEV Community

Cover image for TypeScript Basics: Supercharge Your JavaScript with Strong Typing
Leandro Nuñez
Leandro Nuñez

Posted on

TypeScript Basics: Supercharge Your JavaScript with Strong Typing

Introduction:

Hey there, fellow developers!

Welcome to the world of TypeScript, a powerful superset of JavaScript that brings strong typing and enhanced tooling to our beloved language.

With TypeScript, we can write more robust, maintainable, and error-free code, making our development process smoother than ever.

Today, we'll explore the fundamentals of TypeScript together, with practical examples and a glimpse into how it's used by some of the biggest companies and real-world applications.

What is TypeScript?

TypeScript, developed by Microsoft, is a language that extends JavaScript by adding static typing.

By defining types for our variables, functions, and more, TypeScript helps catch errors during development and enhances code readability.

It's a fantastic choice for large-scale projects, giving developers greater confidence in their code.

Setting Up TypeScript

Before we dive into some examples, let's set up TypeScript in our project.
You can install TypeScript using npm or yarn:

npm install -g typescript
Enter fullscreen mode Exit fullscreen mode

Basic Type Annotations

In TypeScript, we can declare variables with specific types:

let age: number = 25;
let name: string = "John Doe";
let isDeveloper: boolean = true;
Enter fullscreen mode Exit fullscreen mode

Type inference is also available in TypeScript:

let favoriteColor = "blue"; // TypeScript infers favoriteColor as type string
let numberOfItems = 10; // TypeScript infers numberOfItems as type number
Enter fullscreen mode Exit fullscreen mode

Functions with Types

When defining functions, TypeScript allows us to specify parameter types and return types:

function add(a: number, b: number): number {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Interfaces

Interfaces help us define the structure of objects:

interface Person {
  name: string;
  age: number;
}

let person: Person = {
  name: "Alice",
  age: 30,
};
Enter fullscreen mode Exit fullscreen mode

Classes

TypeScript supports classes, allowing us to create object blueprints with methods and properties:

class Dog {
  breed: string;

  constructor(breed: string) {
    this.breed = breed;
  }

  bark() {
    console.log("Woof! Woof!");
  }
}

let labrador = new Dog("Labrador");
labrador.bark(); // Output: "Woof! Woof!"
Enter fullscreen mode Exit fullscreen mode

Top 5 Companies Using TypeScript

TypeScript has gained immense popularity in the tech industry.
Some of the top companies that use TypeScript in their projects are:

  1. Microsoft
  2. Google
  3. Facebook
  4. Airbnb
  5. Slack

Real-World Use Cases

Now, let's explore some common real-world use cases for TypeScript:

1. Web Development:

TypeScript is widely used in frontend web development to build robust and scalable applications.
With frameworks like Angular and React, TypeScript provides developers with enhanced tooling and productivity.

Example:

// Angular Component with TypeScript
import { Component } from "@angular/core";

@Component({
  selector: "app-hello",
  template: `<h1>Hello, {{ name }}!</h1>`,
})
export class HelloComponent {
  name: string = "TypeScript Developer";
}
Enter fullscreen mode Exit fullscreen mode

2. Node.js Applications:

In the backend world, TypeScript is becoming increasingly popular for building server-side applications with Node.js. It offers better code organization and enables developers to catch errors early in the development process.

Example:

// Express.js Server with TypeScript
import express from "express";

const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello, TypeScript!");
});

app.listen(port, () => {
  console.log(`Server started on http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

3. Cross-Platform Mobile Apps:

When building cross-platform mobile apps using frameworks like React Native or NativeScript, TypeScript helps ensure consistency and type safety across different platforms.

Example:

// React Native Component with TypeScript
import React from "react";
import { Text, View } from "react-native";

interface Props {
  name: string;
}

const HelloComponent: React.FC<Props> = ({ name }) => {
  return (
    <View>
      <Text>Hello, {name}!</Text>
    </View>
  );
};
Enter fullscreen mode Exit fullscreen mode

4. Desktop Applications:

Even in the realm of desktop applications, TypeScript is finding its way with the help of Electron, making it easier to develop and maintain complex applications.

Example:

// Electron App with TypeScript
import { app, BrowserWindow } from "electron";

let mainWindow: BrowserWindow | null = null;

app.on("ready", () => {
  mainWindow = new BrowserWindow({ width: 800, height: 600 });
  mainWindow.loadFile("index.html");
  mainWindow.on("closed", () => (mainWindow = null));
});
Enter fullscreen mode Exit fullscreen mode

5. Large-Scale Enterprise Projects:

For large-scale enterprise projects, TypeScript's static typing and clear code structure contribute to better team collaboration and code maintainability.

Conclusion

Congratulations!
We've covered the basics of TypeScript, seen practical examples, and explored real-world use cases.

Embracing TypeScript can transform your JavaScript development experience, providing you with more confidence and efficiency.

So, let's welcome TypeScript, explore its advanced features, and embark on a coding journey filled with powerful, type-safe, and bug-free JavaScript!

References:

I hope you find this blog post helpful in your TypeScript journey.

Happy coding!

Top comments (0)