TypeScript Assignment- 2

Basic Questions

  1. Create an array of numbers [10, 20, 30] in TypeScript and log each number using a for…of loop
  2. Write a TypeScript program that declares a string array [“apple”, “banana”, “mango”] and prints the first element.
  3. Define a tuple [string, number] for storing a student’s name and roll number.
  4. Create a tuple [string, boolean] for storing a username and active status.
  5. Write a tuple [string, number, boolean] with fixed length and print values.
  6. Create a tuple with an optional element [string, number?].
  7. Define a numeric enum Directions with members Up, Down, Left, Right.
  8. Create a string enum Roles with values “Admin”, “User”, “Guest”.
  9. Write a program to access enum values using both index and name.
  10. Use a numeric enum to assign custom values (e.g., Monday = 1, Tuesday = 5).
  11. Create a type alias ID = string | number. Declare variables using it.
  12. Write a type alias Point = {x: number, y: number} and create a point object.
  13. Create a reusable type alias Status = “success” | “error” | “pending”.
  14. Write a simple interface Person with properties name (string) and age (number).
  15. Add an optional property email?: string to Person interface.
  16. Add a readonly property id: number to an interface.
  17. Create two interfaces Student and Teacher and extend Person.
  18. Create an object that implements the Person interface.
  19. Create a TypeScript program that defines an interface Book with properties title (string), author (string), and publishedYear (number). Then create an object implementing this interface and log its details.
  20. Write a function that takes a parameter of type Person interface and logs name.

Intermediate Questions

  1. Write a TypeScript function that accepts an array of strings and returns the length of each string in a new array.
  2. Write a function that accepts a typed array and returns the sum of all values.
  3. Create a tuple [string, number, string] and destructure its values into variables.
  4. Demonstrate rest elements in tuple [number, …string[]].
  5. Write a tuple [string, number, boolean?] and assign with and without optional element.
  6. Create a numeric enum HttpStatus with values like OK=200, NotFound=404.
  7. Create a string enum PaymentStatus with values “Paid”, “Pending”, “Failed”.
  8. Write a function that accepts an enum PaymentStatus and prints a message.
  9. Write a TypeScript function reverseArray that accepts a generic typed array <T[]> and returns a new array with elements in reverse order. Demonstrate it with number[] and string[]
  10. Create a type alias Result = {success: true, data: string} | {success: false, error: string}.
  11. Create a type alias Coordinates = [number, number] and write a function to calculate distance.
  12. Write a type alias that uses intersection (Employee & Manager).
  13. Create an interface Car with brand and speed, and extend it with ElectricCar.
  14. Create an interface with a method signature greet(message: string): void.
  15. Write an interface with an index signature for string keys and number values.
  16. Create an interface Laptop with properties brand: string, model: string. Then extend it in an interface GamingLaptop adding gpu: string. Implement an object of GamingLaptop and log all properties.
  17. Create an interface with readonly array property.
  18. Write a function that accepts either a type alias or interface as parameter.
  19. Write a TypeScript function that accepts a tuple [id: number, name: string, isActive: boolean] and logs each item with its data type.
  20. Write a function that converts enum values into an array of strings.

Advanced Questions

  1. Write a function in TypeScript that takes an array of numbers and returns the largest number.
  2. Create a tuple [string, number, …boolean[]] and use it in a function.
  3. Build a tuple type for representing RGB color [number, number, number] and validate it.
  4. Write a function that takes a tuple [x: number, y: number] and calculates distance from origin.
  5. Create a real-world enum FileAccess (Read=1, Write=2, ReadWrite=3) and use bitwise operations.
  6. Write a function that maps string enum values to numeric codes.
  7. Create a type alias for ApiResponse<T> that handles both success and error. Use generics.
  8. Write a program combining intersection and union types with type aliases for user roles.
  9. Define an interface Database with connect(), disconnect(), and query(sql: string): any. Implement it in a class.
  10. Create a generic Stack class in TypeScript that supports push, pop, and getSize methods. Demonstrate it with a stack of numbers and a stack of strings.