Master TypeScript From Scratch

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

TypeScript Data Types

Learn how TypeScript data types work, including string, number, boolean, arrays, tuples, objects, any, unknown, void, null, undefined, and union types with practical examples and expected outputs.

Simple Explanation: A data type describes the kind of value a variable can store. TypeScript uses types to identify incompatible assignments and help developers write safer code.

What Will We Learn?

TypeScript provides several built-in types and ways to describe values used in an application.

  • Understand basic data types.
  • Work with arrays and tuples.
  • Define object structures using types.
  • Understand any and unknown types.
  • Learn void, null, and undefined.
  • Combine types using union types.

TypeScript Data Type Workflow

Follow these steps to understand how TypeScript uses data types in a program.

📝
Step 1
Declare

Declare a variable.

→
🔤
Step 2
Specify Type

Define the expected value.

→
✅
Step 3
Type Check

Check compatible assignments.

→
🚀
Step 4
Execute

Run the compiled program.

1. String Data Type

The string type is used to represent text, such as names, addresses, course names, and messages. String values can be enclosed in single or double quotes.

Example

let studentName: string = "Rahul";
let courseName: string = "TypeScript";

console.log(studentName);
console.log(courseName);

Expected Output

Rahul
TypeScript

Both variables contain text. TypeScript checks that values assigned to these explicitly typed variables are strings.

2. Number Data Type

The number type represents numeric values, including integers, decimals, and negative numbers. TypeScript uses one main numeric type called number.

Example

let age: number = 22;
let courseFees: number = 35000;
let percentage: number = 85.5;

console.log("Age: " + age);
console.log("Course Fees: " + courseFees);
console.log("Percentage: " + percentage);

Expected Output

Age: 22
Course Fees: 35000
Percentage: 85.5
Remember: TypeScript does not use separate standard types named int and float. Both integer and decimal values use number.

3. Boolean Data Type

The boolean type represents a logical value. It accepts only true or false.

Example

let isEnrolled: boolean = true;
let hasPaidFees: boolean = false;

console.log("Enrolled: " + isEnrolled);
console.log("Fees Paid: " + hasPaidFees);

Expected Output

Enrolled: true
Fees Paid: false

Boolean values are commonly used in conditions, login checks, payment status, and application settings.

4. Array Data Type

An array stores multiple values in one variable. In TypeScript, you can specify the type of elements allowed in an array.

Example

let marks: number[] = [75, 85, 90];
let students: string[] = ["Rahul", "Priya", "Amit"];

console.log(marks);
console.log(students);
console.log(marks[0]);

Expected Output

[ 75, 85, 90 ]
[ 'Rahul', 'Priya', 'Amit' ]
75

Array indexes start at 0. Therefore, marks[0] accesses the first element. Exact formatting of displayed arrays depends on the runtime.

5. Tuple Data Type

A tuple allows you to describe an array with a fixed sequence of element types. Each position has a specified type.

Example

let student: [number, string] = [101, "Priya"];

console.log("Student ID: " + student[0]);
console.log("Student Name: " + student[1]);

Expected Output

Student ID: 101
Student Name: Priya

In this tuple, the first position contains a number and the second position contains a string.

6. Object Data Type

Objects group related information into properties. TypeScript can describe the expected name and type of each property.

Example

let student: {
    id: number;
    name: string;
    course: string;
} = {
    id: 101,
    name: "Amit",
    course: "Dot Net Full Stack"
};

console.log(student.id);
console.log(student.name);
console.log(student.course);

Expected Output

101
Amit
Dot Net Full Stack

This object describes a student using three properties. The compiler checks the declared structure and property types.

7. Any Data Type

The any type allows a variable to hold values of different types while largely opting out of TypeScript's type checking for that value.

Example

let data: any = "Hello";

console.log(data);

data = 100;
console.log(data);

data = true;
console.log(data);

Expected Output

Hello
100
true
Best Practice: Avoid using any unnecessarily because it weakens type safety. Consider unknown when the value's type is not yet known.

8. Unknown Data Type

The unknown type can hold values of any type, but TypeScript requires the value to be checked or narrowed before using it as a more specific type.

Example

let result: unknown = "TypeScript Training";

if (typeof result === "string") {
    console.log(result.toUpperCase());
}

Expected Output

TYPESCRIPT TRAINING

The typeof check confirms that result is a string inside the condition, allowing the string method to be used safely.

9. Void Data Type

The void type is commonly used as the return type of a function that does not return a meaningful value.

Example

function displayMessage(): void {
    console.log("Welcome to TypeScript");
}

displayMessage();

Expected Output

Welcome to TypeScript

This function displays a message and does not return a meaningful result to the caller.

10. Null and Undefined

null represents an intentionally absent value, while undefined commonly represents a value that has not been assigned.

Example

let selectedCourse: string | null = null;
let studentAddress: undefined | string = undefined;

console.log(selectedCourse);
console.log(studentAddress);

Expected Output

null
undefined
Remember: With strictNullChecks enabled, TypeScript requires you to account for null and undefined explicitly when using them with other types.

11. Union Data Type

A union type allows a variable to hold a value of one of several specified types. The pipe symbol | separates the allowed types.

Example

let studentId: number | string;

studentId = 101;
console.log(studentId);

studentId = "CIIT2026";
console.log(studentId);

Expected Output

101
CIIT2026

The variable can store either a number or a string. Other types are not accepted unless they are included in the union.

12. Data Types Comparison

Type Example Usage
string "Rahul" Text values
number 35000 Numeric values
boolean true Logical values
number[] [70, 80, 90] Collection of numbers
[number, string] [101, "Priya"] Tuple with defined positions
any let value: any Opt out of much of type checking
unknown let value: unknown Value requires checking before specific use
void function show(): void Functions without meaningful return values
null null Explicitly absent value
undefined undefined Value not assigned or absent
number | string 101 or "101" Union of number and string

Practical Example: CIIT Student Details

Let's combine different data types in a student registration example.

Example

const instituteName: string = "CIIT Training Institute";

let studentId: number | string = 101;
let studentName: string = "Priya";
let courseFees: number = 35000;
let isRegistered: boolean = true;
let skills: string[] = ["C#", "SQL", "TypeScript"];

console.log("Institute: " + instituteName);
console.log("Student ID: " + studentId);
console.log("Student Name: " + studentName);
console.log("Course Fees: " + courseFees);
console.log("Registered: " + isRegistered);
console.log("First Skill: " + skills[0]);

Expected Output

Institute: CIIT Training Institute
Student ID: 101
Student Name: Priya
Course Fees: 35000
Registered: true
First Skill: C#

This example demonstrates strings, numbers, booleans, arrays, and a union type in one practical program.

Common Mistakes

  • Assigning a string to a variable declared as a number.
  • Using any everywhere instead of specific types.
  • Using an unknown value without checking its type.
  • Accessing an array index that does not exist.
  • Forgetting that tuple positions have specified types.
  • Ignoring possible null or undefined values.
Tip: Prefer specific types such as string, number, and boolean. Use union types when a value genuinely supports multiple types.

Practice Task

Create a TypeScript program for a course registration system using the following requirements.

  • Student name: Sneha
  • Student ID: 202
  • Course fees: 30000
  • Payment completed: false
  • Skills: Java, SQL, TypeScript
  • Selected course: string or null

Choose suitable types for each variable and display the details using console.log().

Hint

let studentName: string = "Sneha";
let studentId: number = 202;
let fees: number = 30000;
let paymentCompleted: boolean = false;
let skills: string[] = ["Java", "SQL", "TypeScript"];
let selectedCourse: string | null = null;

console.log(studentName);
console.log(studentId);
console.log(fees);
console.log(paymentCompleted);
console.log(skills);
console.log(selectedCourse);

Summary

In this lesson, we learned about TypeScript data types, including strings, numbers, booleans, arrays, tuples, objects, any, unknown, void, null, undefined, and union types.

Key Point: Selecting appropriate types helps TypeScript identify incompatible assignments and makes code easier to understand.