TypeScript Assignment- 4

Basic Questions

  1. Write a generic function identity that takes a value of any type and returns the same value.
  2. Create a generic function wrapInArray that accepts a single value and returns it wrapped inside an array.
  3. Define a generic function getFirstElement that returns the first element of a given array of any type.
  4. Create a generic function echo<T>(value: T): T that simply returns its parameter.
  5. Define a generic type Box<T> that holds a single value of type T.
  6. Write a function getLength<T>(arr: T[]): number that returns the length of any type of array.
  7. Define a generic interface Response<T> with data: T and error: string | null.
  8. Create a generic function reverseArray<T>(items: T[]): T[] that reverses any array.
  9. Implement a generic function getLastElement<T>(arr: T[]): T | undefined.
  10. Write a generic function mergeObjects<T, U>(obj1: T, obj2: U) that returns a merged object.
  11. Create a generic class Container<T> that stores and retrieves one item.
  12. Define a generic interface KeyValue<K, V> with properties key: K and value: V.
  13. Create a generic type Nullable<T> that makes a type either T or null.
  14. Define a generic function toArray<T>(value: T): T[] that wraps the value in an array.
  15. Write a generic function findMax<T extends number>(arr: T[]): number.
  16. Create a generic type Pair<T, U> that holds two values of different types.
  17. Write a function printAll<T>(arr: T[]): void that prints all elements of an array.
  18. Define a generic type ReadonlyArray<T> that makes all elements immutable.
  19. Use Partial<T> utility type to make all properties of a User interface optional.
  20. Use Required<T> utility type to make all properties of a Profile interface required.

Intermediate Questions

  1. Write a generic function filterArray<T>(arr: T[], predicate: (item: T) => boolean): T[].
  2. Create a generic class Stack<T> with push, pop, and peek methods.
  3. Define a generic function swap<T, U>(a: T, b: U): [U, T].
  4. Write a generic function findIndex<T>(arr: T[], value: T): number.
  5. Create a generic function mapArray<T, U>(arr: T[], fn: (item: T) => U): U[].
  6. Define a generic interface Repository<T> with methods add, getAll.
  7. Implement a generic function pluck<T, K extends keyof T>(items: T[], key: K): T[K][].
  8. Create a generic function merge<T, U>(obj1: T, obj2: U): T & U.
  9. Write a generic function unique<T>(arr: T[]): T[] that removes duplicates.
  10. Create a generic function countOccurrences<T>(arr: T[], value: T): number.
  11. Define a generic class Dictionary<K, V> with set, get, and has methods.
  12. Implement a generic function groupBy<T, K extends keyof any>(arr: T[], keyGetter: (item: T) => K): Record<K, T[]>.
  13. Create a generic function getProperty<T, K extends keyof T>(obj: T, key: K): T[K].
  14. Define a generic type ReadonlyObject<T> that makes all properties readonly.
  15. Use Pick<T, K> utility type to extract only certain fields from an interface.
  16. Use Omit<T, K> utility type to exclude specific properties from an interface.
  17. Create a generic function flatten<T>(arr: T[][]): T[].
  18. Write a generic function zip<T, U>(arr1: T[], arr2: U[]): [T, U][].
  19. Define a generic type Record<K extends string, V> that represents a dictionary.
  20. Use Readonly<T> utility type to make a Car interface immutable.

Advanced Questions

  1. Create a generic function deepClone<T>(obj: T): T that performs a deep copy.
  2. Write a generic function compose<T, U, V>(f: (a: T) => U, g: (b: U) => V): (a: T) => V.
  3. Implement a generic function partition<T>(arr: T[], predicate: (item: T) => boolean): [T[], T[]].
  4. Create a generic class Cache<K, V> that supports adding, retrieving, and expiring items.
  5. Write a generic function debounce<T extends (…args: any[]) => void>(fn: T, delay: number): T.
  6. Create a generic function throttle<T extends (…args: any[]) => void>(fn: T, limit: number): T.
  7. Implement a generic function retry<T>(fn: () => Promise<T>, retries: number): Promise<T>.
  8. Write a generic type DeepPartial<T> that recursively makes all properties optional.
  9. Create a generic type DeepReadonly<T> that makes all properties deeply readonly.
  10. Implement a generic function safeAccess<T, K extends keyof T>(obj: T, key: K): T[K] | undefined.