30 TypeScript Basic Exercises for Advanced with Solutions
Master advanced TypeScript skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in TypeScript, setting a solid foundation for professional-level challenges. Start your journey to TypeScript mastery today!
Learning Objectives:
Master complex TypeScript concepts such as advanced generics, utility types, decorators, and type manipulation. Build robust, scalable, and maintainable code suitable for real-world, enterprise-level TypeScript applications.
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Write a function to perform a deep clone of an object.
Required Input:
{ name: "John", age: 30, details: { city: "New York", skills: ["TypeScript", "JavaScript"] } }
Expected Output:
Original: {
name: 'John',
age: 30,
details: { city: 'New York', skills: [ 'TypeScript', 'JavaScript' ] }
}
Clone: {
name: 'John',
age: 30,
details: { city: 'New York', skills: [ 'TypeScript', 'JavaScript' ] }
}
Code In Typescript
Run Code?
Click Run Button to view compiled output
2. Implement a singleton class in TypeScript.
Required Input:
None
Expected Output:
Singleton instance created
Are both instances equal? true
Code In Typescript
Run Code?
Click Run Button to view compiled output
3. Write a program to solve the Tower of Hanoi problem for n disks.
Required Input:
n = 3
Expected Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Code In Typescript
Run Code?
Click Run Button to view compiled output
4. Create a function to generate all permutations of a given string.
Required Input:
"abc"
Expected Output:
[ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]
Code In Typescript
Run Code?
Click Run Button to view compiled output
5. Write a program to find the shortest path in an unweighted graph using BFS.
Required Input:
Graph: { A: ["B", "C"], B: ["A", "D", "E"], C: ["A", "F"], D: ["B"], E: ["B", "F"], F: ["C", "E"] }
Start: "A", Target: "F"
Expected Output:
Shortest Path: [ 'A', 'C', 'F' ]
Code In Typescript
Run Code?
Click Run Button to view compiled output
6. Create a function that memoizes the result of a computationally expensive function.
Required Input:
A function f(x) = x * x and input 5 called twice.
Expected Output:
Result from computation
25
Result from cache
25
Code In Typescript
Run Code?
Click Run Button to view compiled output
7. Implement a custom event emitter class with methods on, emit, and off.
Required Input:
Register and emit an event hello.
Expected Output:
Hello, World!
Code In Typescript
Run Code?
Click Run Button to view compiled output
8. Write a function to parse a query string into an object.
Required Input:
"name=John&age=30&city=NewYork"
Expected Output:
{ name: 'John', age: '30', city: 'NewYork' }
Code In Typescript
Run Code?
Click Run Button to view compiled output
9. Write a function to merge two sorted arrays into one sorted array without duplicates.
Required Input:
Array 1: [1, 2, 4, 5]
Array 2: [2, 3, 4, 6]
Expected Output:
[ 1, 2, 3, 4, 5, 6 ]
Code In Typescript
Run Code?
Click Run Button to view compiled output
10. Write a function to validate a deeply nested object structure against a given schema.
Required Input:
Object: { name: "John", details: { age: 30, city: "New York" } }
Schema: { name: "string", details: { age: "number", city: "string" } }
Expected Output:
Object is valid: true
Code In Typescript
Run Code?
Click Run Button to view compiled output