Master TypeScript From Scratch

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

TypeScript Fundamentals

Learn the fundamental concepts of TypeScript, including variables, data types, type inference, type assertions, and constants with practical examples.

Simple Explanation: TypeScript Fundamentals are the basic building blocks used to write structured, readable, and type-safe TypeScript programs.

What Will You Learn?

In this module, we will learn how to declare variables, specify data types, understand automatic type detection, and work with constants.

  • Declare variables using let and var.
  • Work with string, number, boolean, array, and object types.
  • Understand TypeScript type inference.
  • Use type assertions when necessary.
  • Declare constants using const.

TypeScript Fundamentals Workflow

Follow these concepts in order to understand how TypeScript variables and types work.

📝
Step 1
Variables

Store values in variables.

→
🔤
Step 2
Data Types

Specify the kind of data.

→
🧠
Step 3
Type Inference

Let TypeScript infer types.

→
✅
Step 4
Type Safety

Write code with type checks.

1. Variables in TypeScript

Variables are used to store values that can be accessed and used throughout a program. TypeScript supports let, const, and var.

Example

let studentName: string = "Rahul";
let age: number = 22;

console.log(studentName);
console.log(age);

Expected Output

Rahul
22

The let keyword declares variables whose values can be reassigned. TypeScript checks that assigned values are compatible with their declared types.

2. Data Types in TypeScript

Data types describe what kind of values a variable can contain. TypeScript can check whether values are compatible with the specified types.

Example

let courseName: string = "Full Stack Development";
let fees: number = 35000;
let isOnline: boolean = true;

console.log(courseName);
console.log(fees);
console.log(isOnline);

Expected Output

Full Stack Development
35000
true

Common Types

Data Type Example Purpose
string "Rahul" Stores text.
number 100 Stores numeric values.
boolean true Stores true or false.
number[] [70, 80, 90] Stores an array of numbers.
object { age: 22 } Represents an object with properties.

3. Type Inference

Type inference means TypeScript automatically determines a variable's type from its initial value when it can do so reliably.

Example

let studentName = "Priya";
let marks = 90;
let isPassed = true;

console.log(studentName);
console.log(marks);
console.log(isPassed);

Expected Output

Priya
90
true

In this example, TypeScript infers the types as string, number, and boolean, respectively.

Remember: You do not need to write explicit type annotations for every variable. Type inference helps keep code readable while preserving type checking.

4. Type Assertions

A type assertion tells TypeScript to treat a value as a specified type. It does not convert the value or perform runtime type checking by itself.

Example

let message: unknown = "Welcome to TypeScript";

let messageLength: number = (message as string).length;

console.log(messageLength);

Expected Output

22

The assertion message as string tells TypeScript to treat the value as a string for this expression. Use assertions only when you know the underlying value has the required type.

5. Constants in TypeScript

The const keyword declares a block-scoped variable that cannot be reassigned after initialization.

Example

const instituteName: string = "CIIT Training Institute";
const courseFees: number = 35000;

console.log(instituteName);
console.log(courseFees);

Expected Output

CIIT Training Institute
35000
Important: A const object cannot be reassigned, but its properties may still be changed unless additional restrictions are used.

6. Difference Between let, const, and var

Keyword Scope Can Reassign? Can Redeclare in Same Scope?
var Function-scoped Yes Yes
let Block-scoped Yes No
const Block-scoped No No
Best Practice: Prefer const when a variable does not need reassignment, and use let when reassignment is required. Avoid var in most modern code.

Practical Example: Student Course Details

Combine variables, data types, type inference, and constants in one practical program.

Example

const instituteName = "CIIT Training Institute";

let studentName: string = "Amit";
let courseName: string = "Dot Net Full Stack";
let fees: number = 35000;
let isEnrolled: boolean = true;

console.log("Institute: " + instituteName);
console.log("Student: " + studentName);
console.log("Course: " + courseName);
console.log("Fees: " + fees);
console.log("Enrolled: " + isEnrolled);

Expected Output

Institute: CIIT Training Institute
Student: Amit
Course: Dot Net Full Stack
Fees: 35000
Enrolled: true

This program demonstrates how different variable types can be used together in a simple student registration example.

Common Mistakes

  • Assigning a string to a variable declared as a number.
  • Trying to reassign a variable declared using const.
  • Using a type assertion without confirming the actual value.
  • Assuming that type assertions convert values at runtime.
  • Using any unnecessarily and losing useful type checking.

Practice Task

Create a TypeScript program to display student details.

  • Student Name: Sneha
  • Age: 21
  • Course: Java Full Stack
  • Fees: 30000
  • Is Enrolled: true

Use const for a value that should not be reassigned and declare suitable types for the other variables. Display all values using console.log().

Challenge: Try writing the code yourself before checking it with your trainer.

Summary

In this module, we introduced variables, data types, type inference, type assertions, and constants. These concepts form the foundation for writing TypeScript applications.

Next Step: Learn how to declare, initialize, update, and use variables in detail.