TypeScript Assignment- 2
Basic Questions
- Create an array of numbers [10, 20, 30] in TypeScript and log each number using a for…of loop
- Write a TypeScript program that declares a string array [“apple”, “banana”, “mango”] and prints the first element.
- Define a tuple [string, number] for storing a student’s name and roll number.
- Create a tuple [string, boolean] for storing a username and active status.
- Write a tuple [string, number, boolean] with fixed length and print values.
- Create a tuple with an optional element [string, number?].
- Define a numeric enum Directions with members Up, Down, Left, Right.
- Create a string enum Roles with values “Admin”, “User”, “Guest”.
- Write a program to access enum values using both index and name.
- Use a numeric enum to assign custom values (e.g., Monday = 1, Tuesday = 5).
- Create a type alias ID = string | number. Declare variables using it.
- Write a type alias Point = {x: number, y: number} and create a point object.
- Create a reusable type alias Status = “success” | “error” | “pending”.
- Write a simple interface Person with properties name (string) and age (number).
- Add an optional property email?: string to Person interface.
- Add a readonly property id: number to an interface.
- Create two interfaces Student and Teacher and extend Person.
- Create an object that implements the Person interface.
- 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.
- Write a function that takes a parameter of type Person interface and logs name.
Intermediate Questions
- Write a TypeScript function that accepts an array of strings and returns the length of each string in a new array.
- Write a function that accepts a typed array and returns the sum of all values.
- Create a tuple [string, number, string] and destructure its values into variables.
- Demonstrate rest elements in tuple [number, …string[]].
- Write a tuple [string, number, boolean?] and assign with and without optional element.
- Create a numeric enum HttpStatus with values like OK=200, NotFound=404.
- Create a string enum PaymentStatus with values “Paid”, “Pending”, “Failed”.
- Write a function that accepts an enum PaymentStatus and prints a message.
- 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[]
- Create a type alias Result = {success: true, data: string} | {success: false, error: string}.
- Create a type alias Coordinates = [number, number] and write a function to calculate distance.
- Write a type alias that uses intersection (Employee & Manager).
- Create an interface Car with brand and speed, and extend it with ElectricCar.
- Create an interface with a method signature greet(message: string): void.
- Write an interface with an index signature for string keys and number values.
- 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.
- Create an interface with readonly array property.
- Write a function that accepts either a type alias or interface as parameter.
- Write a TypeScript function that accepts a tuple [id: number, name: string, isActive: boolean] and logs each item with its data type.
- Write a function that converts enum values into an array of strings.
Advanced Questions
- Write a function in TypeScript that takes an array of numbers and returns the largest number.
- Create a tuple [string, number, …boolean[]] and use it in a function.
- Build a tuple type for representing RGB color [number, number, number] and validate it.
- Write a function that takes a tuple [x: number, y: number] and calculates distance from origin.
- Create a real-world enum FileAccess (Read=1, Write=2, ReadWrite=3) and use bitwise operations.
- Write a function that maps string enum values to numeric codes.
- Create a type alias for ApiResponse<T> that handles both success and error. Use generics.
- Write a program combining intersection and union types with type aliases for user roles.
- Define an interface Database with connect(), disconnect(), and query(sql: string): any. Implement it in a class.
- 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.