Objective: Write a TypeScript program that outputs a welcome message.
Instructions:
- Create a TypeScript program.
- Print the following message to the console:
Hello World, I will complete this course successfully and become a Next level Web Developer!
Objective: Create a function with parameters and an optional literal type.
Instructions:
- Define a function that takes:
name(string)age(number)role(optional, with type'admin' | 'user' | 'guest')
- The function should log these values or perform a basic action.
Objective: Define a structured Person object using Type Aliases.
Instructions:
- Define a
Persontype alias with properties forName,Address,Hair and Eye Color,Income and Expense,Hobbies,Family Members,Job,Skills,Marital Status, andFriends.
Objective: Create union and intersection types using interfaces.
Instructions:
- Define interfaces
BookandMagazine. - Create:
- A type that is a union of
BookandMagazine. - A type that is an intersection of
BookandMagazine.
- A type that is a union of
Objective: Write a function that reverses a string.
Instructions:
- Write a function
reverseStringthat:- Takes a single string argument.
- Returns the string in reverse order.
- Example:
- Input:
"hello" - Output:
"olleh"
- Input:
Objective: Write a function that uses the rest operator for variable-length arguments.
Instructions:
- Create a function that takes multiple numeric arguments (using the rest operator) and returns the sum of all arguments.
Objective: Write a function that behaves differently based on the input type.
Instructions:
- Create a function that accepts a parameter of type
string | number. - The function should:
- Return the length if the input is a string.
- Return the square if the input is a number.
Objective: Practice using intersection types.
Instructions:
- Create a type
AdminUserthat is an intersection of:Userwith propertiesnameandemailAdminwith propertyadminLevel
- Write a function
describeAdmin(user: AdminUser): stringthat returns a description of the admin user.
Objective: Use optional chaining with nested objects.
Instructions:
- Write a function
getEmployeeCity(employee)that safely retrieves the city of an employee from a nested object using optional chaining.
Objective: Handle null and undefined values using nullish coalescing.
Instructions:
- Write a function
getDisplayName(name: string | null | undefined): stringthat returns"Anonymous"ifnameis null or undefined.
Objective: Handle different types with the unknown type.
Instructions:
- Write a function
processData(data: unknown)that:- Checks if
datais a string and returns the uppercased version. - If
datais a number, returns the square of it.
- Checks if
Objective: Use the never type for functions that don’t return.
Instructions:
- Write a function
handleErrorthat:- Accepts a
message: string. - Throws an error with the given message.
- Use the
neverreturn type to indicate that this function never returns.
- Accepts a
Objective: Use generics to handle different types.
Instructions:
- Create a generic function that:
- Accepts an array of any type.
- Returns a new array with duplicate values removed.
Objective: Simulate an asynchronous operation with TypeScript.
Instructions:
- Write an asynchronous function that:
- Simulates fetching user data (containing
nameandage). - Returns the data after a short delay.
- Simulates fetching user data (containing
Objective: Create custom type guards for more accurate type checking.
Instructions:
- Write a function
isString(value: unknown): value is stringthat checks if a value is a string. - Use this in another function
printUpperCase(value: unknown): voidthat prints the value in uppercase if it’s a string.
Objective: Access object properties dynamically using keyof.
Instructions:
- Create a function that:
- Takes an object and a key.
- Returns the property value from the object based on the provided key.
- Use
keyofto constrain the key to valid properties of the object.
Happy coding!