TypeScript Assignment- 1
Basic Questions
- 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.
- Write a TypeScript program to declare variables of type string, number, and boolean.
- Create a variable with type annotation any and assign different types of values to it.
- Create a variable with type annotation unknown and demonstrate type checking before using it.
- Write a function that takes a string as a parameter and returns its length.
- Declare a variable with explicit type annotation and another one with type inference. Compare both.
- Write a program where a function accepts number | string (union type) and logs its type.
- Create an intersection type combining {name: string} and {age: number} into one object.
- Declare a function with return type void that logs a message.
- Write a function that throws an error and annotate its return type as never.
- Create a function with default parameters and demonstrate type inference.
- Define a literal type 1 | 2 | 3 and create a function that only accepts these numbers.
- Use union literals to restrict a variable to values “small” | “medium” | “large”.
- Write a function with type annotation on both parameters and return type.
- Create a nullable variable (string | null). Assign both values and log them.
- Use typeof to infer type from a variable and reuse it in another variable.
- Demonstrate type inference by assigning a value without type annotation.
- Write a function that accepts an optional parameter and returns a string.
- Create an array of type number[] and another of type string[].
- Write a function that accepts a boolean parameter and prints “Yes” if true, “No” otherwise.
Intermediate Questions
- 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.
- Create a union type Dog | Cat with different properties and a function to handle both.
- Define an intersection type Employee & Manager with common and unique fields.
- Write a program where a function accepts union type “circle” | “square” and calculates area.
- Create a function that accepts string | number and converts it into a string.
- Define a literal type “success” | “error” | “loading” and build a function that handles each case.
- Create a custom type alias for union type string | number | boolean.
- Use a type alias with an intersection type combining User and Address.
- Write a program to use unknown type safely with typeof checks before accessing it.
- Demonstrate a function that takes two parameters, one with type annotation and one inferred.
- Create a type-safe object { id: number, name: string, isActive: boolean }.
- Write a TypeScript program using never type for exhaustive type checking in a switch-case.
- Create a function that accepts union type string | null | undefined and handles all cases.
- Write a program that uses literal type “on” | “off” to control a toggle function.
- Use a void return type to define a logging function that prints a timestamped message.
- Write a TypeScript program to demonstrate type inference with arrays ([1,2,3]).
- Build a function that merges two intersection types and returns the combined object.
- Create a function that demonstrates difference between any and unknown.
- Write a function that only accepts numbers between 1 | 2 | 3 | 4 | 5.
- Create a TypeScript project with tsconfig.json and compile a .ts file into .js.
Advanced Questions
- Write a TypeScript program that defines:A union type CreditCard | PayPal where both have different fields,
- Build a function that takes a union type parameter (string | number) and performs different logic.
- Create a class Person with type annotations for properties and methods.
- Define an interface Car with properties and implement it in an object using TypeScript.
- Write a function that enforces input to be of type “add” | “remove” | “update”.
- Create a type-safe function that accepts an object and returns all its keys as an array of strings.
- Write a function using intersection types to combine two objects (User & Profile).
- Define a literal type “read” | “write” | “execute”, and build an access control function.
- Write a TypeScript program that validates a user object with union and intersection types combined.
- Build a CLI-based TypeScript program where user selects options “start” | “stop” | “exit”.