FSD-II Practical Assignment 5
5.1Understand how generics can enhance type safety and code reusability
in TypeScript -Create a TypeScript function that accepts an array of any
type and returns the first element of that array.
npm install -g typescript
genericfunction.ts
function getFirstElement<T>(array: T[]): T | undefined {
return array[0];
}
// Using the function with a number array
const numbers = [1, 2, 3, 4];
const firstNumber = getFirstElement(numbers);
console.log(firstNumber); // Output: 1
// Using the function with a string array
const strings = ["apple", "banana", "cherry"];
const firstString = getFirstElement(strings);
console.log(firstString); // Output: "apple"
// Using the function with an array of objects
const objects = [{ id: 1 }, { id: 2 }, { id: 3 }];
const firstObject = getFirstElement(objects);
console.log(firstObject); // Output: { id: 1 }
PS C:\Users\admin\angular> tsc genericfunction.ts
PS C:\Users\admin\angular> node genericfunction.js
1
apple
{ id: 1 }
2. - Refactor the function to use generics to enforce type safety.
// Function to get the first element of an array
function getFirstElement<T>(array: T[]): T | undefined {
if (array.length === 0) {
return undefined; // Explicitly return undefined if the array is empty
}
return array[0];
}
// Using the function with different types
// Array of numbers
const numbers = [1, 2, 3, 4];
const firstNumber = getFirstElement(numbers);
console.log(firstNumber); // Output: 1
// Array of strings
const strings = ["apple", "banana", "cherry"];
const firstString = getFirstElement(strings);
console.log(firstString); // Output: "apple"
// Array of objects
const objects = [{ id: 1 }, { id: 2 }, { id: 3 }];
const firstObject = getFirstElement(objects);
console.log(firstObject); // Output: { id: 1 }
// Empty array
const emptyArray: number[] = [];
const firstElement = getFirstElement(emptyArray);
console.log(firstElement); // Output: undefined
Output
PS C:\Users\admin\angular> tsc genericfunction.ts
PS C:\Users\admin\angular> node genericfunction.js
1
apple
{ id: 1 }
Undefined
2.Create a generic class that implements a basic stack data structure (push,
pop, peek). - Test the stack class with different data types.
// Stack.ts
export class Stack<T> {
private items: T[] = [];
// Add an item to the stack
push(item: T): void {
this.items.push(item);
}
// Remove and return the top item from the stack
pop(): T | undefined {
return this.items.pop();
}
// Return the top item without removing it
peek(): T | undefined {
return this.items[this.items.length - 1];
}
// Return the size of the stack
size(): number {
return this.items.length;
}
// Check if the stack is empty
isEmpty(): boolean {
return this.size() === 0;
}
}
// Stack.test.ts
import { Stack } from './Stack';
// Test with numbers
const numberStack = new Stack<number>();
numberStack.push(1);
numberStack.push(2);
numberStack.push(3);
console.log('Number Stack');
console.log('Peek:', numberStack.peek()); // 3
console.log('Pop:', numberStack.pop()); // 3
console.log('Size:', numberStack.size()); // 2
// Test with strings
const stringStack = new Stack<string>();
stringStack.push('a');
stringStack.push('b');
stringStack.push('c');
console.log('String Stack');
console.log('Peek:', stringStack.peek()); // 'c'
console.log('Pop:', stringStack.pop()); // 'c'
console.log('Size:', stringStack.size()); // 2
// Test with objects
const objectStack = new Stack<{ id: number; name: string }>();
objectStack.push({ id: 1, name: 'Object1' });
objectStack.push({ id: 2, name: 'Object2' });
objectStack.push({ id: 3, name: 'Object3' });
console.log('Object Stack');
console.log('Peek:', objectStack.peek()); // { id: 3, name: 'Object3' }
console.log('Pop:', objectStack.pop()); // { id: 3, name: 'Object3' }
console.log('Size:', objectStack.size()); // 2
Output-
PS C:\Users\admin\angular> tsc Stack.test.ts
PS C:\Users\admin\angular> node Stack.test.js
Number Stack
Peek: 3
Pop: 3
Size: 2
String Stack
Peek: c
Pop: c
Size: 2
Object Stack
Peek: { id: 3, name: 'Object3' }
Pop: { id: 3, name: 'Object3' }
Size: 2
PS C:\Users\admin\angular>
Comments
Post a Comment