Skip to main content
Angular Basics
CHAPTER 04 Beginner

TypeScript Basics for Angular

Updated: May 18, 2026
5 min read

# CHAPTER 4

TypeScript Basics for Angular

1. Chapter Introduction

Angular is built entirely on TypeScript. While standard JavaScript allows you to do almost anything without complaining (like adding a Number to a String), this freedom causes massive bugs in large enterprise applications. TypeScript fixes this by adding Static Typing to JavaScript. If you try to pass text into a math function, TypeScript will throw a red error in VS Code *before* you even run the app. In this chapter, we will cover the essential TypeScript features you need to write Angular Components safely.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare variables with explicit Types (string, number, boolean).
  • Write functions with typed parameters and return types.
  • Create structured Data Models using Interfaces.
  • Understand Object-Oriented Classes and Constructors.
  • Understand Access Modifiers (public, private).

3. Variables and Data Types

In JavaScript, you declare variables using let or const. In TypeScript, you do the same, but you append a colon : followed by the data type.
typescript
1234567
// Strongly typed variables
let username: string = "Alice";
let age: number = 28;
let isActive: boolean = true;

// The compiler throws an error if you violate the type!
// age = "Thirty"; // ERROR: Type 'string' is not assignable to type 'number'.

Type Inference: You don't *always* have to write the type. If you assign a value immediately, TypeScript is smart enough to guess the type.

typescript
1
let city = "London"; // Inferred as string automatically

4. Typed Arrays

If you want to create a list, you must specify what type of data the list holds by using Type[].
typescript
1234
let scores: number[] = [95, 88, 100];
let names: string[] = ["John", "Jane"];

// names.push(5); // ERROR: Cannot push a number into a string array.

5. Functions in TypeScript

Functions require types for both their parameters and their return value.
typescript
123456789
// (a is a number, b is a number) : returns a number
function addNumbers(a: number, b: number): number {
    return a + b;
}

// If a function returns nothing, its return type is 'void'
function logMessage(message: string): void {
    console.log(message);
}

6. Interfaces (Creating Custom Data Models)

When fetching data from an API (like a user profile), we need a way to tell Angular exactly what shape that data has. We use Interfaces. They do not exist in standard JavaScript.
typescript
1234567891011121314
// Define the shape of a User
interface User {
    id: number;
    name: string;
    email: string;
    isAdmin?: boolean; // The '?' makes this property optional
}

// Enforce the Interface on an object
let currentUser: User = {
    id: 101,
    name: "Alice",
    email: "alice@example.com"
};

If you spell "email" wrong, or forget the "id", VS Code will immediately underline currentUser in red! This is the superpower of TypeScript.

7. Classes and Object-Oriented Programming

Angular relies heavily on Object-Oriented Programming (OOP). Every Component and Service you write in Angular will be a class.
typescript
1234567891011121314151617181920
class Product {
    // Properties
    title: string;
    price: number;

    // Constructor runs when the object is created
    constructor(title: string, price: number) {
        this.title = title;
        this.price = price;
    }

    // Method
    getDetails(): string {
        return `${this.title} costs $${this.price}`;
    }
}

// Instantiating the class
let laptop = new Product("MacBook", 1500);
console.log(laptop.getDetails());

8. Access Modifiers (public, private)

TypeScript adds security to your classes.
  • public (Default): Can be accessed from anywhere (including the HTML template).
  • private: Can only be used inside the Class itself. The HTML template cannot see it!
typescript
12345678
class BankAccount {
    public accountHolder: string = "Alice";
    private balance: number = 5000; // Hidden from outside world!

    public deposit(amount: number): void {
        this.balance += amount;
    }
}

*(Note: Angular provides a massive shortcut. If you put public or private inside the constructor parameters, TypeScript automatically creates the properties and assigns them for you! We will see this constantly in Angular Services).*

9. Common Mistakes

  • Abusing the any type: If you don't know what type a variable is, you can label it as any. This turns off type-checking entirely. Writing let data: any = ... is considered terrible practice and defeats the purpose of using TypeScript.
  • Forgetting this. in classes: In Java/C#, you can often access class properties directly. In TypeScript, you MUST prefix class properties with this. (e.g., this.balance = 5).

10. MCQs with Answers

Question 1

What is the primary benefit of using TypeScript over JavaScript?

Question 2

How do you correctly declare a number variable in TypeScript?

Q3. If you write let name = "John"; without explicit typing, does TypeScript know it is a string? a) Yes, through Type Inference b) No, it defaults to any Answer: a) Yes, through Type Inference.
Question 4

What is the return type of a function that does not return anything?

Question 5

What TypeScript feature allows you to define the exact "shape" of an object (e.g., specifying that a User must have an id and a name)?

Question 6

How do you mark a property inside an Interface as optional?

Question 7

Every Angular Component is fundamentally built using which TypeScript feature?

Question 8

What access modifier restricts a property so it can only be accessed from within its own class?

Question 9

Which access modifier must be used if you want a Component's variable to be readable by the HTML template?

Question 10

Why is using the any type heavily discouraged in Angular development?

11. Interview Questions

  • Q: Explain what an Interface is in TypeScript and why it is useful when working with APIs. (Answer: Interfaces define strict contracts for data shapes. When an API returns a JSON object, an Interface guarantees that the developer accesses existing properties correctly, preventing "undefined" crashes).
  • Q: What is the difference between public and private variables in an Angular Component class?

12. Summary

TypeScript is the bedrock of Angular. By forcing developers to define the shapes of their data using Interfaces, specifying parameter types in functions, and utilizing Class-based OOP architecture, TypeScript guarantees a level of stability and predictability that vanilla JavaScript cannot provide.

13. Next Chapter Recommendation

Now that we understand the language, we can start building UI! In Chapter 5: Angular Components, we will create our very first Component, explore the @Component decorator, and learn how to snap UI pieces together.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·