Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript vs JavaScript

Understand the differences between TypeScript and JavaScript, including typing, error detection, compilation, development tools, and real-world applications.

Simple Explanation: JavaScript is a programming language that runs in JavaScript environments. TypeScript extends JavaScript with a type system and is commonly compiled into JavaScript before execution.

What Is the Difference?

JavaScript is widely used to build interactive websites, frontend applications, and backend applications. It supports dynamic typing, which means variables can hold values of different types during execution.

TypeScript builds on JavaScript by adding features such as static type checking, type annotations, interfaces, and generics. These features help developers identify many type-related problems during development.

CIIT Example: Imagine a student registration application. JavaScript can handle student details, while TypeScript can additionally define expected types for student names, IDs, course fees, and other application data.

TypeScript vs JavaScript Comparison

Feature JavaScript TypeScript
Typing Dynamically typed Provides static type checking
Type Annotations Does not support TypeScript type annotations Supports type annotations
Error Detection Many errors appear during execution Many type-related errors can be detected during development
Compilation Usually runs directly in a JavaScript environment Commonly compiled into JavaScript
Interfaces No TypeScript-style interface declarations Supports interfaces for defining object shapes
Development Tools Supported by modern JavaScript editors Offers rich type-aware editor features
Learning Learn JavaScript syntax and concepts Requires JavaScript knowledge plus TypeScript concepts
Runtime Runs in compatible JavaScript environments Its emitted JavaScript runs in compatible environments

1. JavaScript Example

JavaScript allows variables to receive values of different types. This flexibility is useful, but it can also cause unexpected results when the code assumes a particular type.

Example

let marks = 85;

marks = "Eighty Five";

console.log(marks);

Output

Eighty Five

JavaScript allows the variable to change from a number to a string because it is dynamically typed.

2. TypeScript Example

TypeScript allows developers to declare the expected type of a variable. The compiler can report incompatible assignments during type checking.

Example

let marks: number = 85;

marks = 90;

console.log(marks);

Output

90

Type Error Example

let marks: number = 85;

// TypeScript reports a type error.
marks = "Eighty Five";
Expected Result: The assignment is incompatible with the declared number type, so TypeScript reports a type-checking error.

3. Function Comparison

TypeScript lets developers declare parameter and return types. This makes the expected input and output of a function clearer.

JavaScript Example

function add(a, b) {
    return a + b;
}

console.log(add(10, 20));

Output

30

TypeScript Example

function add(a: number, b: number): number {
    return a + b;
}

console.log(add(10, 20));

Output

30

In the TypeScript example, both parameters and the return value are declared as numbers.

4. Object Structure

Both JavaScript and TypeScript support objects. TypeScript additionally allows developers to describe object shapes through interfaces and other type definitions.

JavaScript Example

const student = {
    id: 101,
    name: "Priya",
    course: "Java Full Stack"
};

console.log(student.name);

Output

Priya

TypeScript Example

interface Student {
    id: number;
    name: string;
    course: string;
}

const student: Student = {
    id: 101,
    name: "Priya",
    course: "Java Full Stack"
};

console.log(student.name);

Output

Priya
Explanation: The Student interface specifies the expected types of the properties. It helps TypeScript check the object structure.

5. Compilation Difference

JavaScript is commonly executed directly by a browser or another JavaScript runtime. TypeScript is commonly compiled into JavaScript before it is executed.

TypeScript File: app.ts

let instituteName: string = "CIIT";
console.log(instituteName);

Compilation Command

tsc app.ts

Possible JavaScript Output: app.js

let instituteName = "CIIT";
console.log(instituteName);

Program Output

CIIT

The generated JavaScript can be executed in a compatible JavaScript environment. The exact emitted syntax depends on the TypeScript compiler configuration.

When Should You Use JavaScript or TypeScript?

Scenario Suitable Choice Reason
Learning basic programming and web development JavaScript Helps build a foundation in core web programming concepts.
A small script or simple interactive page JavaScript or TypeScript The choice depends on project needs and tooling.
Large application with many contributors TypeScript can be beneficial Types can improve code navigation and maintainability.
Application with complex data structures TypeScript can be beneficial Interfaces and types can describe expected data shapes.
Existing JavaScript project Either, depending on project requirements TypeScript can be adopted gradually where appropriate.

Practical Example: CIIT Student Details

The following TypeScript example defines student details using an interface and displays the student's name and course.

Example

interface StudentDetails {
    studentId: number;
    studentName: string;
    courseName: string;
}

function displayStudent(student: StudentDetails): void {
    console.log("Student ID: " + student.studentId);
    console.log("Student Name: " + student.studentName);
    console.log("Course: " + student.courseName);
}

const student: StudentDetails = {
    studentId: 101,
    studentName: "Amit",
    courseName: "Dot Net Full Stack"
};

displayStudent(student);

Output

Student ID: 101
Student Name: Amit
Course: Dot Net Full Stack
Real-World Use: Similar types can help organize student registration data, course information, and enquiry details in a web application.

Common Misunderstandings

  • TypeScript does not replace JavaScript; it builds on JavaScript.
  • TypeScript does not guarantee that an application has no runtime errors.
  • TypeScript types generally do not remain in emitted JavaScript.
  • JavaScript is not an inferior language; it remains useful for many applications.
  • TypeScript commonly introduces a compilation or build step into the development workflow.
  • External data may still require runtime validation, even in a TypeScript application.

Summary

JavaScript provides dynamic typing and is used throughout web development. TypeScript extends JavaScript with static type checking, interfaces, and additional developer tools. TypeScript can be especially helpful for larger applications and shared codebases, while JavaScript remains suitable for projects of many sizes. The right choice depends on the application and its development requirements.