WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TypeScript · Intermediate · question 23 of 100

What is the use of namespaces in TypeScript and how do they help in code organization?

📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

Namespaces in TypeScript are a way to organize code and provide a scoped container for different constructs (like classes, interfaces, and functions). They help in structuring the codebase and avoiding naming conflicts, making it easier to maintain the application as it grows.

Using namespaces, you can group your code based on specific functionalities or features, and thus improve code organization and readability.

Here’s an example of a namespace in TypeScript:

namespace Animals {
  export class Dog {
    constructor(public name: string) {}

    bark() {
      return 'Woof, woof!';
    }
  }

  export class Cat {
    constructor(public name: string) {}

    meow() {
      return 'Meow, meow!';
    }
  }
}

In the example above, we’ve created a ‘Animals‘ namespace that contains ‘Dog‘ and ‘Cat‘ classes. By using the ‘export‘ keyword, we’ve made these classes accessible from outside the namespace.

To use these classes, you’d refer to them using the namespace:

let myDog = new Animals.Dog('Buddy');
console.log(myDog.name); // Output: Buddy
console.log(myDog.bark()); // Output: Woof, woof!

let myCat = new Animals.Cat('Whiskers');
console.log(myCat.name); // Output: Whiskers
console.log(myCat.meow()); // Output: Meow, meow!

This example illustrates how namespaces help in keeping the different components of your code organized and scoped, avoiding naming conflicts.

However, it’s important to note that with the widespread adoption of ES modules in JavaScript, using modules (which can be imported in a similar way to namespaces) is becoming the preferred way to structure and organize the code.

Here’s the previous example using ES modules instead:

// animals.ts
export class Dog {
  constructor(public name: string) {}

  bark() {
    return 'Woof, woof!';
  }
}

export class Cat {
  constructor(public name: string) {}

  meow() {
    return 'Meow, meow!';
  }
}

To use these classes, you’d import them using the module syntax:

import { Dog, Cat } from './animals';

let myDog = new Dog('Buddy');
console.log(myDog.name); // Output: Buddy
console.log(myDog.bark()); // Output: Woof, woof!

let myCat = new Cat('Whiskers');
console.log(myCat.name); // Output: Whiskers
console.log(myCat.meow()); // Output: Meow, meow!

In summary, namespaces are a way to organize code, avoid naming conflicts, and provide a scoped container for different constructs in TypeScript. However, ES modules are becoming the preferred way to structure and organize code, providing similar benefits with a more modern syntax.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TypeScript interview — then scores it.
📞 Practice TypeScript — free 15 min
📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

All 100 TypeScript questions · All topics