TypeScript Circular Dependencies
TypeScript Modules and Namespaces : Exercise-5 with Solution
Write two TypeScript modules that import from each other, creating a circular dependency. Show how TypeScript handles circular dependencies and uses the imported functions or classes.
Sample Solution:
TypeScript Code:
main.ts
import { sayHelloFromA } from './test_module_a';
sayHelloFromA();
test_module_a.ts
import { sayHelloFromB } from './test_module_b';
export function sayHelloFromA() {
console.log('Hello from module A');
sayHelloFromB();
}
test_module_b.ts
import { sayHelloFromA } from './test_module_a';
export function sayHelloFromB() {
console.log('Hello from module B');
}
Output:
Hello from module A Hello from module B
TypeScript Editor:
See the Pen TypeScript by w3resource (@w3resource) on CodePen.
Previous: TypeScript Default Export.
Next: TypeScript Basic Namespace.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/typescript-exercises/typescript-modules-and-namespaces-exercise-5.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics