0
0
Typescriptprogramming~15 mins

Generic array syntax in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Generic Array Syntax in TypeScript
📖 Scenario: You are building a simple program to manage a list of favorite fruits. You want to use TypeScript's generic array syntax to store and work with this list safely.
🎯 Goal: Create a TypeScript program that uses generic array syntax to store a list of fruits, add a new fruit, and then display the list.
📋 What You'll Learn
Create a generic array of strings to hold fruit names
Add a new fruit to the array
Use a for loop to iterate over the array
Print each fruit name
💡 Why This Matters
🌍 Real World
Generic arrays help you store lists of items like names, products, or tasks safely with type checking.
💼 Career
Understanding generic arrays is essential for writing clean, error-free TypeScript code in web development and software engineering.
Progress0 / 4 steps
1
Create a generic array of fruits
Create a variable called fruits that is a generic array of strings with these exact values: 'Apple', 'Banana', 'Cherry'.
Typescript
Need a hint?

Use let fruits: Array = [...] to create the array.

2
Add a new fruit to the array
Add the string 'Date' to the fruits array using the push method.
Typescript
Need a hint?

Use fruits.push('Date') to add the new fruit.

3
Loop through the fruits array
Use a for loop with the variable fruit to iterate over fruits and collect each fruit name into a new array called fruitList.
Typescript
Need a hint?

Use for (let fruit of fruits) { fruitList.push(fruit); } to copy fruits.

4
Print the fruit list
Use console.log to print the fruitList array.
Typescript
Need a hint?

Use console.log(fruitList) to show the list.