TypeScript Assignment- 4
Basic Questions
- Write a generic function identity that takes a value of any type and returns the same value.
- Create a generic function wrapInArray that accepts a single value and returns it wrapped inside an array.
- Define a generic function getFirstElement that returns the first element of a given array of any type.
- Create a generic function echo<T>(value: T): T that simply returns its parameter.
- Define a generic type Box<T> that holds a single value of type T.
- Write a function getLength<T>(arr: T[]): number that returns the length of any type of array.
- Define a generic interface Response<T> with data: T and error: string | null.
- Create a generic function reverseArray<T>(items: T[]): T[] that reverses any array.
- Implement a generic function getLastElement<T>(arr: T[]): T | undefined.
- Write a generic function mergeObjects<T, U>(obj1: T, obj2: U) that returns a merged object.
- Create a generic class Container<T> that stores and retrieves one item.
- Define a generic interface KeyValue<K, V> with properties key: K and value: V.
- Create a generic type Nullable<T> that makes a type either T or null.
- Define a generic function toArray<T>(value: T): T[] that wraps the value in an array.
- Write a generic function findMax<T extends number>(arr: T[]): number.
- Create a generic type Pair<T, U> that holds two values of different types.
- Write a function printAll<T>(arr: T[]): void that prints all elements of an array.
- Define a generic type ReadonlyArray<T> that makes all elements immutable.
- Use Partial<T> utility type to make all properties of a User interface optional.
- Use Required<T> utility type to make all properties of a Profile interface required.
Intermediate Questions
- Write a generic function filterArray<T>(arr: T[], predicate: (item: T) => boolean): T[].
- Create a generic class Stack<T> with push, pop, and peek methods.
- Define a generic function swap<T, U>(a: T, b: U): [U, T].
- Write a generic function findIndex<T>(arr: T[], value: T): number.
- Create a generic function mapArray<T, U>(arr: T[], fn: (item: T) => U): U[].
- Define a generic interface Repository<T> with methods add, getAll.
- Implement a generic function pluck<T, K extends keyof T>(items: T[], key: K): T[K][].
- Create a generic function merge<T, U>(obj1: T, obj2: U): T & U.
- Write a generic function unique<T>(arr: T[]): T[] that removes duplicates.
- Create a generic function countOccurrences<T>(arr: T[], value: T): number.
- Define a generic class Dictionary<K, V> with set, get, and has methods.
- Implement a generic function groupBy<T, K extends keyof any>(arr: T[], keyGetter: (item: T) => K): Record<K, T[]>.
- Create a generic function getProperty<T, K extends keyof T>(obj: T, key: K): T[K].
- Define a generic type ReadonlyObject<T> that makes all properties readonly.
- Use Pick<T, K> utility type to extract only certain fields from an interface.
- Use Omit<T, K> utility type to exclude specific properties from an interface.
- Create a generic function flatten<T>(arr: T[][]): T[].
- Write a generic function zip<T, U>(arr1: T[], arr2: U[]): [T, U][].
- Define a generic type Record<K extends string, V> that represents a dictionary.
- Use Readonly<T> utility type to make a Car interface immutable.
Advanced Questions
- Create a generic function deepClone<T>(obj: T): T that performs a deep copy.
- Write a generic function compose<T, U, V>(f: (a: T) => U, g: (b: U) => V): (a: T) => V.
- Implement a generic function partition<T>(arr: T[], predicate: (item: T) => boolean): [T[], T[]].
- Create a generic class Cache<K, V> that supports adding, retrieving, and expiring items.
- Write a generic function debounce<T extends (…args: any[]) => void>(fn: T, delay: number): T.
- Create a generic function throttle<T extends (…args: any[]) => void>(fn: T, limit: number): T.
- Implement a generic function retry<T>(fn: () => Promise<T>, retries: number): Promise<T>.
- Write a generic type DeepPartial<T> that recursively makes all properties optional.
- Create a generic type DeepReadonly<T> that makes all properties deeply readonly.
- Implement a generic function safeAccess<T, K extends keyof T>(obj: T, key: K): T[K] | undefined.