Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Certified Typescript Programming Expert Β· Lesson 3 of 16

Module Two

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3
Module 2 Β· Certified TypeScript Programming Expert Β· Beginners

πŸ“˜ Module Two: Basic Types – The Building Blocks of TypeScript


πŸ“– Module Introduction

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!


🎯 Learning Objectives

By the end of this module, you will be able to:

  • Understand what basic types are.
  • Use string, number, and boolean types.
  • Work with arrays and tuples.
  • Understand the 'any' and 'void' types.
  • Write TypeScript code with proper types.

πŸ“š Warm-up Story: The Label That Saved the Day

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!"


πŸ“˜ Main Lessons

Lesson 1: What are Types?

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.

Lesson 2: The String Type

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.

Lesson 3: The Number Type

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.

Lesson 4: The Boolean Type

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.

Lesson 5: The Array Type

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.

Lesson 6: The Tuple Type

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.

Lesson 7: The Any Type

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.

Lesson 8: The Void Type

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.

Lesson 9: Type Annotations

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.

Lesson 10: Type Inference

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.

Lesson 11: Using Types with Functions

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.

Lesson 12: Common Mistakes with Types

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.

Lesson 13: Best Practices for Types

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.

Lesson 14: Types in Everyday Life

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.

Lesson 15: You – The Type Master

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!


πŸ“– Key Vocabulary

WordSimple Definition
TypeA label for values.
StringText in programming.
NumberA numeric value.
BooleanTrue or false.
ArrayA list of values.
TupleA fixed-size list.
AnyCan hold any value.
VoidNo return value.
AnnotationYou write the type.
InferenceTypeScript guesses the type.

🧩 Important Concepts

  • Types are labels for values.
  • String, number, and boolean are basic types.
  • Arrays hold lists of values.
  • Tuples are fixed-size lists.
  • You are now a type master.

πŸ”’ Step-by-Step Explanations

How to Use Types in TypeScript (in 4 steps)

  1. Step 1: Decide what kind of value you need (text, number, true/false).
  2. Step 2: Choose the appropriate type (string, number, boolean).
  3. Step 3: Write the type annotation or let TypeScript infer it.
  4. Step 4: Use the variable in your code.

🌍 Real-life Examples

  • User profile: name (string), age (number), isActive (boolean).
  • Product: name (string), price (number), inStock (boolean).
  • Student: name (string), grade (number), passed (boolean).
  • Transaction: amount (number), description (string), isApproved (boolean).
  • Order: id (number), items (array), total (number).

πŸ‡³πŸ‡¬ Nigerian Examples

  • User: name (string), age (number), city (string).
  • Product: name (string), price (number), isAvailable (boolean).
  • Student: name (string), grade (number), isPassing (boolean).
  • Transaction: amount (number), description (string), isCompleted (boolean).
  • Order: id (number), items (array), total (number).

🎈 Fun Examples Children Can Relate To

  • Pet: name (string), age (number), isFriendly (boolean).
  • Game: name (string), level (number), isCompleted (boolean).
  • Friend: name (string), age (number), likesPizza (boolean).
  • Book: title (string), pages (number), isRead (boolean).
  • Movie: title (string), rating (number), isWatched (boolean).

🏠 Everyday Examples

  • Grocery: item (string), quantity (number), isBought (boolean).
  • Chore: task (string), priority (number), isDone (boolean).
  • Contact: name (string), phone (string), isFavorite (boolean).
  • Note: title (string), content (string), isImportant (boolean).
  • Expense: amount (number), category (string), isPaid (boolean).

πŸ‘©β€πŸ« Teacher Notes

  • This module is about types – keep it simple and practical.
  • Use many examples to illustrate each type.
  • Encourage students to write code with types.
  • Emphasise the importance of types for catching errors.
  • Make it interactive with live coding.

πŸ‘ͺ Parent Tips

  • Encourage your child to think about types in everyday life.
  • Help them understand the difference between text, numbers, and true/false.
  • Support their coding practice.
  • Discuss how types help avoid mistakes.
  • Celebrate their progress.

πŸ’‘ Interesting Facts

  • TypeScript types help catch errors before they happen.
  • Types are like labels – they tell you what's inside.
  • TypeScript can infer types automatically.
  • Types make code easier to understand.
  • Types are used by all large TypeScript projects.

πŸ€” Did You Know?

  • Did you know that strings can be written with single or double quotes?
  • Did you know that numbers can be integers or decimals?
  • Did you know that boolean values are only true or false?
  • Did you know that arrays can hold any type of value?

🧠 Remember This

  • Types are labels for values.
  • String is for text.
  • Number is for numbers.
  • Boolean is for true/false.
  • You are a type master.

⚠️ Common Mistakes

  • Mistake 1: Using a string where a number is expected.
  • Mistake 2: Using a number where a boolean is expected.
  • Mistake 3: Forgetting to use types.
  • Mistake 4: Using any type too much.
  • Mistake 5: Not using type annotations.

βœ… Best Practices

  • Always use types.
  • Always use string for text.
  • Always use number for numbers.
  • Always use boolean for true/false.
  • Always avoid using any.

🎨 ASCII Illustrations

Types – Overview

   Types in TypeScript
       |
       +-- String (text)
       |
       +-- Number (numbers)
       |
       +-- Boolean (true/false)
       |
       +-- Array (list)
       |
       +-- Tuple (fixed list)

Type Annotations – Example

   let name: string = "Ada";
   let age: number = 25;
   let isStudent: boolean = true;
   let grades: number[] = [90, 85, 88];
   let person: [string, number] = ["Ada", 25];

πŸ“Š Comparison Tables

Basic Types

TypeWhat It RepresentsExample
stringText"Hello"
numberNumber123
booleanTrue/Falsetrue
arrayList of items[1,2,3]
tupleFixed-size list["Ada", 25]

Any vs Void

TypeUsed ForExample
anyAny valuelet a: any = "hello";
voidNo return valuefunction greet(): void {}

πŸ“ Summary After Every Lesson

(Included after each lesson above – review them!)


πŸ“Œ End-of-Module Summary

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!


❓ Frequently Asked Questions

  1. What is a type? A label for values.
  2. What is a string? Text.
  3. What is a number? A numeric value.
  4. What is a boolean? True or false.
  5. What is an array? A list of values.
  6. What is a tuple? A fixed-size list.
  7. What is any? Any value.
  8. What is void? No return value.
  9. What is type inference? TypeScript guessing the type.
  10. What is a type annotation? Writing the type.

πŸ“ Review Questions (15)

  1. What is a type?
  2. What is the string type?
  3. What is the number type?
  4. What is the boolean type?
  5. What is an array?
  6. What is a tuple?
  7. What is the any type?
  8. What is the void type?
  9. What is a type annotation?
  10. What is type inference?
  11. How do you use an array?
  12. How do you use a tuple?
  13. What is a common mistake?
  14. What is a best practice?
  15. What makes you a type master?

✏️ Fill-in-the-Blank Exercises

  1. A __________ is a label for values.
  2. The __________ type is for text.
  3. The __________ type is for numbers.
  4. The __________ type is for true/false.
  5. An __________ is a list of values.
  6. A __________ is a fixed-size list.
  7. The __________ type can hold any value.
  8. The __________ type means no return value.
  9. Type __________ means writing the type.
  10. Type __________ means TypeScript guesses the type.

βœ… True or False Exercises

  1. A string is for text. (True)
  2. A number is for true/false. (False)
  3. A boolean is for true/false. (True)
  4. An array is a fixed-size list. (False)
  5. A tuple is a fixed-size list. (True)
  6. The any type can hold any value. (True)
  7. The void type can hold any value. (False)
  8. Type annotations are optional. (False – best to use them)
  9. Type inference means writing the type. (False)
  10. You are a type master. (True)

πŸ”˜ Multiple Choice Questions (15 with answers)

  1. What is a type?
    a) A label for values
    b) A game
    c) A video
    Answer: a
  2. What is a string?
    a) Text
    b) Number
    c) True/False
    Answer: a
  3. What is a number?
    a) Numeric value
    b) Text
    c) True/False
    Answer: a
  4. What is a boolean?
    a) True/False
    b) Text
    c) Number
    Answer: a
  5. What is an array?
    a) A list of values
    b) A fixed-size list
    c) A single value
    Answer: a
  6. What is a tuple?
    a) A fixed-size list
    b) A list of values
    c) A single value
    Answer: a
  7. What is any?
    a) Can hold any value
    b) No value
    c) Only numbers
    Answer: a
  8. What is void?
    a) No return value
    b) Any value
    c) Only numbers
    Answer: a
  9. What is a type annotation?
    a) Writing the type
    b) Guessing the type
    c) Ignoring the type
    Answer: a
  10. What is type inference?
    a) Guessing the type
    b) Writing the type
    c) Ignoring the type
    Answer: a
  11. What is a common mistake?
    a) Using a string for a number
    b) Using a number for a string
    c) Using the correct type
    Answer: a
  12. What is a best practice?
    a) Use types
    b) Ignore types
    c) Use any
    Answer: a
  13. What is the purpose of types?
    a) Catch errors
    b) Slow down code
    c) Confuse developers
    Answer: a
  14. Can TypeScript infer types?
    a) Yes
    b) No
    Answer: a
  15. Are you a type master?
    a) Yes
    b) No
    Answer: a

πŸ”— Matching Exercises

Match the term with its definition.

TermDefinition
1. StringA. Text
2. NumberB. Numeric value
3. BooleanC. True/False
4. ArrayD. List of values
5. TupleE. Fixed-size list

Answers: 1-A, 2-B, 3-C, 4-D, 5-E


πŸ“ Short Answer Questions

  1. What is a type?
  2. What are the basic types in TypeScript?
  3. What is the difference between an array and a tuple?
  4. What is the any type?
  5. What is the void type?

🎭 Scenario-based Exercises

  1. Scenario 1: You need to store a customer's name, age, and whether they are active. What types would you use?
  2. Scenario 2: You need to store a list of product prices. What type would you use?
  3. Scenario 3: You need to store a product's name and price together. What type would you use?

πŸ‘₯ Group Activity

β€œ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.


πŸ§‘β€πŸ’» Individual Activity

β€œMy Type Examples”

Write TypeScript code using string, number, boolean, array, and tuple. Share your code with the class.


πŸ—£οΈ Classroom Discussion Questions

  1. Why are types useful?
  2. What happens if you use the wrong type?
  3. How can types help catch errors?
  4. What is the difference between any and void?
  5. What would you like to learn next?

πŸ› οΈ Mini Project

β€œ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.


πŸ’» Practical Assignment

β€œWrite TypeScript Code”

Write a TypeScript program that uses string, number, boolean, array, and tuple. Compile and run it. Share your code and output.


⚑ Challenge Exercise

β€œ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.


πŸ“‹ Quiz Answers

(Answers to fill-in-the-blank, true/false, and multiple choice are provided within those sections.)


🎁 Key Takeaways

  • Types are labels for values.
  • String is for text.
  • Number is for numbers.
  • Boolean is for true/false.
  • Arrays and tuples store lists.

πŸš€ Preparation for Module Three

In Module Three, you will learn about interfaces and types. To prepare:

  • Review what you learned about types.
  • Think about how you would describe an object (like a person or product).
  • Bring your questions to the next class.

Get ready to learn about interfaces!


End of Module Two Β· You are now a type master!

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’