TypeScript Basics

Share with a friend:
typescript logo

TypeScript has gained immense popularity among developers due to its ability to enhance the JavaScript programming experience by adding static typing and improved tooling. In this article, we will delve into the basics of TypeScript, its key features, and how it can benefit your development projects.

Introduction to TypeScript

TypeScript is an open-source programming language developed by Microsoft. It is a strict syntactical superset of JavaScript that adds optional static typing. This means that you can write TypeScript code that looks very similar to JavaScript, but with the added benefit of type-checking during development.


This guide will introduce the basics. For an in-depth guide on Typescript visit here.


By introducing static types, TypeScript catches type-related errors at compile-time rather than runtime. This leads to more robust code, better code documentation, and improved developer experience. TypeScript compiles down to plain JavaScript, which can then be executed in any JavaScript runtime environment.

Setting Up TypeScript

To start using TypeScript, you need to set up a development environment. Here’s a basic setup process:

  1. Install Node.js: TypeScript requires Node.js and npm (Node Package Manager). Download and install them from the official website: Node.js.
  2. Install TypeScript: Once Node.js is installed, open your terminal and run the following command to install TypeScript globally:
   npm install -g typescript
  1. Create a TypeScript Project: Create a directory for your project and navigate to it in your terminal. Then, create a tsconfig.json file by running:
   tsc --init

This file will hold TypeScript compiler options and project settings.

  1. Write TypeScript Code: Create a .ts file in your project directory and start writing TypeScript code.
  2. Compile TypeScript: Compile your TypeScdript code to JavaScript by running:
   tsc filename

This command will generate JavaScript files based on your TypeScript code.

Declaring Variables and Types

In TypeScript, you declare variables using the let or const keyword, just like in JavaScript. However, you can also specify the variable type using a colon and the type name.

let message: string = "Hello, TypeScript!";
const age: number = 25;

TypeScript supports various basic types, including number, string, boolean, null, undefined, object. It also supports any and unknown types. For a detailed list of the types supported in Typescript visit here.

Additionally, TypeScript allows you to define custom types using interfaces. We will explore this in the upcoming section.

Interfaces

Interfaces are a powerful feature of TypeScript, allowing you to define the structure of objects and their types.

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

const person: Person = {
    name: "Alice",
    age: 30
};

Do you know the difference between an interface and a type in Typescript? See the answer here.

Functions

Functions in TypeScript are similar to JavaScript functions but with the ability to specify types for parameters and return types.

function add(x: number, y: number): number {
    return x + y;
}

Classes and Inheritance

TypeScript supports object-oriented programming concepts like classes and inheritance. You can create classes with properties, methods, and constructors.

class Animal {
    constructor(public name: string) {}

    makeSound(): void {
        console.log("Some sound");
    }
}

class Dog extends Animal {
    makeSound(): void {
        console.log("Woof!");
    }
}

const dog: Dog = new Dog("Buddy");
dog.makeSound(); // Output: Woof!

TypeScript and Modern JavaScript

TypeScript not only adds static typing but also incorporates the latest ECMAScript features. This means you can use features from ES6, ES7, and beyond while writing TypeScript code.

const numbers: number[] = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);

Are you already familiar with Javascript? Do you think Typescript will replace Javascript?

TypeScript Compiler and Configuration

The TypeScript compiler (tsc) is responsible for converting TypeScript code into JavaScript. The tsconfig.json file we created earlier helps configure how the TypeScript compiler behaves.

You can specify options such as target (the ECMAScript version to compile to), strict (enables all strict type-checking options), outDir (output directory for compiled files), and more. To learn more about Typescript’s configuration file, visit this article.

TypeScript’s Role in Large Projects

TypeScript truly shines in large-scale projects where maintainability and collaboration are crucial. As the codebase grows, TypeScript’s static typing helps catch errors early, reducing the likelihood of bugs and enhancing overall code quality. It also improves code readability and provides better documentation for APIs and interfaces. If you are wondering whether or not to use Typescript in your project, this article may help.

TypeScript Tools and Resources

TypeScript has a thriving ecosystem with various tools and resources to aid developers:

  • TypeScript Playground: An online editor that allows you to experiment with TypeScript code and see the JavaScript output in real-time.
  • DefinitelyTyped: A repository of type definitions for popular JavaScript libraries, providing better type support when using these libraries in TypeScript.
  • VS Code: The popular code editor from Microsoft has excellent TypeScript support, including IntelliSense, debugging, and code navigation features.

There are a lot more tools, libraries and frameworks that Typescript can be used with. To learn how Typescript can be used for React.js/Next.js, visit here. To learn more about how tools such as ESLint and Prettier can be used with Typescript, visit here.

Conclusion

In this article, we’ve explored the fundamental concepts of TypeScript. From its introduction as a statically-typed superset of JavaScript to its benefits in modern web development, TypeScript offers a powerful toolkit for developers to write cleaner, safer, and more maintainable code. By embracing TypeScript, you can enhance your coding experience and create more robust applications. So, why not give TypeScript a try and experience its advantages firsthand? Happy coding! 🚀

Share with a friend:

Rajae Robinson

Rajae Robinson is a young Software Developer with over 3 years of work experience building websites and mobile apps. He has extensive experience with React.js and Next.js.

More Posts on Typescript