TypeScript Assignment- 1

Basic Questions

  1. Write a TypeScript program that declares variables using both let and const, demonstrates type annotation, and tries to reassign them to show how TypeScript enforces rules.
  2. Write a TypeScript program to declare variables of type string, number, and boolean.
  3. Create a variable with type annotation any and assign different types of values to it.
  4. Create a variable with type annotation unknown and demonstrate type checking before using it.
  5. Write a function that takes a string as a parameter and returns its length.
  6. Declare a variable with explicit type annotation and another one with type inference. Compare both.
  7. Write a program where a function accepts number | string (union type) and logs its type.
  8. Create an intersection type combining {name: string} and {age: number} into one object.
  9. Declare a function with return type void that logs a message.
  10. Write a function that throws an error and annotate its return type as never.
  11. Create a function with default parameters and demonstrate type inference.
  12. Define a literal type 1 | 2 | 3 and create a function that only accepts these numbers.
  13. Use union literals to restrict a variable to values “small” | “medium” | “large”.
  14. Write a function with type annotation on both parameters and return type.
  15. Create a nullable variable (string | null). Assign both values and log them.
  16. Use typeof to infer type from a variable and reuse it in another variable.
  17. Demonstrate type inference by assigning a value without type annotation.
  18. Write a function that accepts an optional parameter and returns a string.
  19. Create an array of type number[] and another of type string[].
  20. Write a function that accepts a boolean parameter and prints “Yes” if true, “No” otherwise.

Intermediate Questions

  1. Create a TypeScript program where you define an interface Product with fields: id: number, name: string, price: number, and inStock: boolean. Then write a function that takes an array of Product and returns only the products which are in stock.
  2. Create a union type Dog | Cat with different properties and a function to handle both.
  3. Define an intersection type Employee & Manager with common and unique fields.
  4. Write a program where a function accepts union type “circle” | “square” and calculates area.
  5. Create a function that accepts string | number and converts it into a string.
  6. Define a literal type “success” | “error” | “loading” and build a function that handles each case.
  7. Create a custom type alias for union type string | number | boolean.
  8. Use a type alias with an intersection type combining User and Address.
  9. Write a program to use unknown type safely with typeof checks before accessing it.
  10. Demonstrate a function that takes two parameters, one with type annotation and one inferred.
  11. Create a type-safe object { id: number, name: string, isActive: boolean }.
  12. Write a TypeScript program using never type for exhaustive type checking in a switch-case.
  13. Create a function that accepts union type string | null | undefined and handles all cases.
  14. Write a program that uses literal type “on” | “off” to control a toggle function.
  15. Use a void return type to define a logging function that prints a timestamped message.
  16. Write a TypeScript program to demonstrate type inference with arrays ([1,2,3]).
  17. Build a function that merges two intersection types and returns the combined object.
  18. Create a function that demonstrates difference between any and unknown.
  19. Write a function that only accepts numbers between 1 | 2 | 3 | 4 | 5.
  20. Create a TypeScript project with tsconfig.json and compile a .ts file into .js.

Advanced Questions

  1. Write a TypeScript program that defines:A union type CreditCard | PayPal where both have different fields,
  2. Build a function that takes a union type parameter (string | number) and performs different logic.
  3. Create a class Person with type annotations for properties and methods.
  4. Define an interface Car with properties and implement it in an object using TypeScript.
  5. Write a function that enforces input to be of type “add” | “remove” | “update”.
  6. Create a type-safe function that accepts an object and returns all its keys as an array of strings.
  7. Write a function using intersection types to combine two objects (User & Profile).
  8. Define a literal type “read” | “write” | “execute”, and build an access control function.
  9. Write a TypeScript program that validates a user object with union and intersection types combined.
  10. Build a CLI-based TypeScript program where user selects options “start” | “stop” | “exit”.