TypeScript Basics for Angular
# 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
Classesand Constructors.
-
Understand Access Modifiers (
public,private).
3. Variables and Data Types
In JavaScript, you declare variables usinglet or const. In TypeScript, you do the same, but you append a colon : followed by the data type.
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.
4. Typed Arrays
If you want to create a list, you must specify what type of data the list holds by usingType[].
5. Functions in TypeScript
Functions require types for both their parameters and their return value.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 useInterfaces. They do not exist in standard JavaScript.
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 aclass.
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!
*(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
anytype: If you don't know what type a variable is, you can label it asany. This turns off type-checking entirely. Writinglet 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 withthis.(e.g.,this.balance = 5).
10. MCQs with Answers
What is the primary benefit of using TypeScript over JavaScript?
How do you correctly declare a number variable in TypeScript?
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.
What is the return type of a function that does not return anything?
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)?
How do you mark a property inside an Interface as optional?
Every Angular Component is fundamentally built using which TypeScript feature?
What access modifier restricts a property so it can only be accessed from within its own class?
Which access modifier must be used if you want a Component's variable to be readable by the HTML template?
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
publicandprivatevariables 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.