Hello, TypeScript builder! In Module One, you learned what TypeScript is and how it works. Now it is time to learn about the building blocks of TypeScript β types.
Types are like labels that tell you what kind of value something is. They help you avoid mistakes and make your code safer. In this module, you will learn about the most common types: string, number, boolean, array, and tuple.
Let's start building with TypeScript types!
By the end of this module, you will be able to:
In Lagos, a young developer named Ada was building a shopping app. She had to store customer names, prices, and whether an item was in stock. She wrote her code in JavaScript.
But she made a mistake β she accidentally used a string (text) where she should have used a number (price). The app showed the wrong price, and customers were confused.
Her mentor said, "Ada, you need to use types. TypeScript would have caught this error. Types are like labels β they tell you what kind of value you are using."
Ada switched to TypeScript. She used the string type for names, number for prices, and boolean for stock status. The errors disappeared. Ada said, "Types are like labels β they keep my code organised and safe!"
Definition: A type is a label that tells you what kind of value something is β like a number, text, or true/false.
Why is it important? Types help you avoid mistakes β you can't accidentally use a number as text.
Simple explanation: Types are like labels on boxes β they tell you what's inside.
Real-life example: A number type can only hold numbers.
School example: A student uses types to organise data.
Home example: A parent uses types to keep track of expenses.
Nigerian example: A Nigerian developer uses types for data consistency.
Types: - string: text (e.g., "hello") - number: numbers (e.g., 123) - boolean: true/false
Mini summary: Types tell you what kind of value something is.
Definition: The string type is used for text β like words, sentences, or names.
Why is it important? Most data in programs is text β names, descriptions, and messages.
Simple explanation: A string is anything inside quotes β "hello", "Ada", "123" as text.
Real-life example: "Ada" is a string.
School example: A student's name is a string.
Home example: A family member's name is a string.
Nigerian example: "Lagos" is a string.
let name: string = "Ada"; let city: string = "Lagos";
Mini summary: The string type is for text.
Definition: The number type is used for numbers β like 5, 10, or 3.14.
Why is it important? Numbers are used for prices, ages, quantities, and more.
Simple explanation: A number is any numeric value β positive, negative, or decimal.
Real-life example: A price of β¦500 is a number.
School example: A student's age is a number.
Home example: The number of family members is a number.
Nigerian example: The price of a product is a number.
let age: number = 25; let price: number = 500.50;
Mini summary: The number type is for numbers.
Definition: The boolean type is for true or false values.
Why is it important? Booleans are used for conditions β like "is it in stock?" or "is it available?"
Simple explanation: A boolean is either true or false.
Real-life example: "Is the light on?" β true or false.
School example: "Did you finish your homework?" β true or false.
Home example: "Is the door locked?" β true or false.
Nigerian example: "Is the item in stock?" β true or false.
let isAvailable: boolean = true; let isExpired: boolean = false;
Mini summary: The boolean type is for true or false.
Definition: An array is a list of values β like a list of names or numbers.
Why is it important? Arrays are used to store multiple items in one place.
Simple explanation: An array is like a shopping list β it holds many items.
Real-life example: A list of customer names.
School example: A list of student grades.
Home example: A list of family members.
Nigerian example: A list of product prices.
let names: string[] = ["Ada", "Chidi", "Bola"]; let prices: number[] = [100, 200, 300];
Mini summary: Arrays hold lists of values.
Definition: A tuple is like an array, but with a fixed number of items and types.
Why is it important? Tuples are used when you know exactly how many items and what types.
Simple explanation: A tuple is a fixed-size list with specific types.
Real-life example: A tuple for a person's name and age β ["Ada", 25].
School example: A tuple for a student's name and grade.
Home example: A tuple for a family member's name and age.
Nigerian example: A tuple for a product name and price.
let person: [string, number] = ["Ada", 25]; let product: [string, number] = ["Rice", 500];
Mini summary: Tuples are fixed-size lists with specific types.
Definition: The any type can hold any value β string, number, boolean, or anything.
Why is it important? It is useful when you are not sure what type a value will be.
Simple explanation: Any is like a wildcard β it can be anything.
Real-life example: A variable that can be a string or a number.
School example: A variable that can hold different types of data.
Home example: A variable that can be anything.
Nigerian example: A variable that can hold any value.
let value: any = "hello"; value = 123; // This is allowed
Mini summary: The any type can hold anything.
Definition: The void type is used for functions that do not return anything.
Why is it important? It tells you that a function does not produce a value.
Simple explanation: Void means "nothing" β the function does not return a value.
Real-life example: A function that prints a message but does not return anything.
School example: A function that displays a grade but does not return it.
Home example: A function that displays a message but does not return anything.
Nigerian example: A function that logs a message but does not return a value.
function greet(): void {
console.log("Hello!");
}
Mini summary: The void type means no return value.
Definition: Type annotations are the way you tell TypeScript what type a variable should be.
Why is it important? They make your code clear and help catch errors.
Simple explanation: You write a colon and the type after the variable name.
Real-life example: let name: string = "Ada";
School example: let grade: number = 90;
Home example: let isActive: boolean = true;
Nigerian example: let city: string = "Lagos";
let name: string = "Ada"; let age: number = 25; let isStudent: boolean = true;
Mini summary: Type annotations tell TypeScript what type to use.
Definition: Type inference is when TypeScript automatically guesses the type of a variable.
Why is it important? It saves you from writing the type every time.
Simple explanation: TypeScript is smart β it can often guess the type from the value.
Real-life example: let name = "Ada"; β TypeScript knows it's a string.
School example: let age = 25; β TypeScript knows it's a number.
Home example: let isReady = true; β TypeScript knows it's a boolean.
Nigerian example: let city = "Lagos"; β TypeScript knows it's a string.
let name = "Ada"; // TypeScript infers string let age = 25; // TypeScript infers number
Mini summary: Type inference saves you from writing types.
Definition: Functions can have types for parameters and return values.
Why is it important? It ensures that functions are used correctly.
Simple explanation: You tell TypeScript what types the function expects and returns.
Real-life example: function add(a: number, b: number): number { return a + b; }
School example: function getGrade(name: string): number { ... }
Home example: function calculatePrice(price: number, tax: number): number { ... }
Nigerian example: function getProduct(name: string): string { ... }
function add(a: number, b: number): number {
return a + b;
}
Mini summary: Functions can have type annotations.
Definition: Common mistakes are errors that beginners often make with types.
Why is it important? Avoiding them saves time and frustration.
Simple explanation: Don't assign a string to a number variable.
Real-life example: let age: number = "twenty"; // Error!
School example: let grade: number = "A"; // Error!
Home example: let isReady: boolean = "yes"; // Error!
Nigerian example: let price: number = "five hundred"; // Error!
let age: number = "twenty"; // Type error let name: string = 123; // Type error
Mini summary: Avoid assigning the wrong type to a variable.
Definition: Best practices are the recommended ways to use types.
Why is it important? They make your code clean and safe.
Simple explanation: Always use types β they help catch errors.
Real-life example: Use string for text, number for numbers, boolean for true/false.
School example: Use types to organise your code.
Home example: Use types to avoid mistakes.
Nigerian example: Use types for reliable apps.
let name: string = "Ada"; // Good let age: number = 25; // Good let isStudent: boolean = true; // Good
Mini summary: Use types to make your code safe.
Definition: Types are not just for code β they are everywhere in life.
Why is it important? It shows that types are natural and easy to understand.
Simple explanation: You use types every day β like labels on boxes.
Real-life example: A box labelled "books" holds books.
School example: A folder labelled "homework" holds homework.
Home example: A drawer labelled "socks" holds socks.
Nigerian example: A container labelled "rice" holds rice.
Types are like labels β they tell you what's inside.
Mini summary: Types are natural and easy to understand.
Definition: You are now a type master β you understand the basic types in TypeScript.
Why is it important? You can now write TypeScript code with confidence.
Simple explanation: You have mastered the building blocks of TypeScript.
Real-life example: You can now build simple TypeScript apps.
School example: You can use types in school projects.
Home example: You can practice TypeScript at home.
Nigerian example: You can build apps with TypeScript in Nigeria.
Type Master Checklist: - Understands string, number, boolean - Can use arrays and tuples - Knows any and void - Uses type annotations - Understands type inference
Mini summary: You are now a type master!
| Word | Simple Definition |
|---|---|
| Type | A label for values. |
| String | Text in programming. |
| Number | A numeric value. |
| Boolean | True or false. |
| Array | A list of values. |
| Tuple | A fixed-size list. |
| Any | Can hold any value. |
| Void | No return value. |
| Annotation | You write the type. |
| Inference | TypeScript guesses the type. |
Types in TypeScript
|
+-- String (text)
|
+-- Number (numbers)
|
+-- Boolean (true/false)
|
+-- Array (list)
|
+-- Tuple (fixed list)
let name: string = "Ada"; let age: number = 25; let isStudent: boolean = true; let grades: number[] = [90, 85, 88]; let person: [string, number] = ["Ada", 25];
| Type | What It Represents | Example |
|---|---|---|
| string | Text | "Hello" |
| number | Number | 123 |
| boolean | True/False | true |
| array | List of items | [1,2,3] |
| tuple | Fixed-size list | ["Ada", 25] |
| Type | Used For | Example |
|---|---|---|
| any | Any value | let a: any = "hello"; |
| void | No return value | function greet(): void {} |
(Included after each lesson above β review them!)
In this module, you learned about the basic types in TypeScript. You discovered the string, number, and boolean types. You also learned about arrays, tuples, any, and void. You learned to use type annotations and understand type inference. You are now a type master. In the next module, you will learn about interfaces and types. Congratulations on completing Module Two!
Match the term with its definition.
| Term | Definition |
|---|---|
| 1. String | A. Text |
| 2. Number | B. Numeric value |
| 3. Boolean | C. True/False |
| 4. Array | D. List of values |
| 5. Tuple | E. Fixed-size list |
Answers: 1-A, 2-B, 3-C, 4-D, 5-E
βType Explorationβ
In groups, create a list of everyday items and assign types to each part (name, price, etc.). Share your examples with the class.
βMy Type Examplesβ
Write TypeScript code using string, number, boolean, array, and tuple. Share your code with the class.
βTypeScript Data Modelβ
Create a data model for a business (like a store or a school) using types. Include string, number, boolean, array, and tuple. Present your model to the class.
βWrite TypeScript Codeβ
Write a TypeScript program that uses string, number, boolean, array, and tuple. Compile and run it. Share your code and output.
βType Challengeβ
Create a complex data structure using all the basic types. Include an array of objects, each with string, number, boolean, and tuple properties. Share your solution.
(Answers to fill-in-the-blank, true/false, and multiple choice are provided within those sections.)
In Module Three, you will learn about interfaces and types. To prepare:
Get ready to learn about interfaces!
End of Module Two Β· You are now a type master!