Namespaces Flashcards

1
Q

What is a namespace in TypeScript?

A

In TypeScript, a namespace is a way to group related code. This is a part of TypeScript’s modularity system, which includes modules and namespaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you define a namespace in TypeScript?

A

Namespaces are defined using the namespace keyword followed by the namespace name. Within the namespace, you can define interfaces, classes, functions, and variables. Here’s a simple example:

namespace MyNamespace {
  export class MyClass {
    doSomething() {
      console.log("Doing something...");
    }
  }
}

// Usage
let instance = new MyNamespace.MyClass();
instance.doSomething(); // logs: "Doing something..."

In the code above, MyClass is a part of the MyNamespace namespace. The export keyword is used to make MyClass available outside of the namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why has the use of namespaces in TypeScript been discouraged in recent versions of the language?

A

As of TypeScript 2.0, the use of namespaces is discouraged in favor of ES6 modules. ES6 modules provide a more standard and flexible way to manage code modularity. This approach is more compatible with the wider JavaScript ecosystem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access elements defined in a TypeScript namespace?

A

You can access them using the namespace name followed by a dot and the element name, e.g., MyNamespace.MyClass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the export keyword used for in TypeScript namespaces?

A

The export keyword is used to make elements defined within the namespace (such as classes, interfaces, functions, or variables) available outside of the namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a multi-file namespace in TypeScript?

A

A multi-file namespace is a namespace that is split across multiple files. Each file must use the namespace keyword followed by the namespace name and contains a portion of the namespace’s code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you write a basic example of a multi-file namespace in TypeScript?

A
// File1.ts
namespace SharedNamespace {
  export class ClassFromFirstFile {
    doSomething() {
      console.log("Doing something in first file...");
    }
  }
}

// File2.ts
/// <reference path="File1.ts" />
namespace SharedNamespace {
  export class ClassFromSecondFile {
    doSomethingElse() {
      console.log("Doing something in second file...");
    }
  }
}

Note: Once there are multiple files involved, we’ll need to make sure all of the compiled code gets loaded.

One way to concatenat output is using the outFile option to compile all of the input files into a single JavaScript output file:

tsc --outFile sample.js File2.ts

The compiler will automatically order the output file based on the reference tags present in the files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a nested namespace in TypeScript?

A

A nested namespace in TypeScript is a namespace inside another namespace. This is another way to organize related code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you write a basic example of a nested namespace in TypeScript?

A
namespace OuterNamespace {
  export namespace InnerNamespace {
    export class MyClass {
      doSomething() {
        console.log("Doing something...");
      }
    }
  }
}

// Usage
let instance = new OuterNamespace.InnerNamespace.MyClass();
instance.doSomething(); // logs: "Doing something..."
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is namespace aliasing in TypeScript?

A

Namespace aliasing in TypeScript allows you to create a shorter name or alias for a namespace using the import keyword. It can make your code easier to read and write, particularly when dealing with long or deeply nested namespaces.

Example:

import q = x.y.z;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can you write a basic example of namespace aliasing in TypeScript?

A
namespace VeryLongNamespaceName {
  export class MyClass {
    doSomething() {
      console.log("Doing something...");
    }
  }
}

// Creating an alias
import Alias = VeryLongNamespaceName;

// Usage
let instance = new Alias.MyClass();
instance.doSomething(); // logs: "Doing something..."

In this example, Alias is an alias for VeryLongNamespaceName, allowing us to use the shorter name when referring to the namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are Ambient Namespaces in TypeScript?

A

Ambient namespaces are used in TypeScript to define types for existing JavaScript libraries that don’t have TypeScript definitions. They provide a way to describe the types of the library’s objects. Ambient namespaces are declared in ambient context using declare namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you write a basic example of an Ambient Namespace in TypeScript?

A
// Assuming there is a library called 'myLib' in your project

declare namespace myLib {
    function doSomething(value: string): string;
    let someValue: number;
}

In this example, an ambient namespace myLib is defined to describe a JavaScript library of the same name. The doSomething function and someValue variable are defined with types as they would be used in the library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How are Ambient Namespaces used in TypeScript?

A

Ambient Namespaces are typically used in TypeScript Declaration Files (.d.ts files), where they provide type information about an existing library. In the consuming TypeScript code, you can then use the library as if it were written in TypeScript, with type checking and autocomplete working correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Please explain what this ambient namespace does

declare namespace D3 {
  export interface Selectors {
    select: {
      (selector: string): Selection;
      (element: EventTarget): Selection;
    };
  }
  export interface Event {
    x: number;
    y: number;
  }
  export interface Base extends Selectors {
    event: Event;
  }
}
declare var d3: D3.Base;
A

This TypeScript code is declaring an ambient namespace for the D3.js library.

Here’s a breakdown of what this code is doing:

  • D3 is the namespace that contains all of the type declarations. The declare namespace D3 indicates that these types are describing objects in the D3.js library.
  • Selectors is an interface inside the D3 namespace. It describes an object that has a select function. The select function can take either a string or EventTarget as an argument and it returns a Selection.
  • Event is another interface inside the D3 namespace. It describes an object that has x and y properties, both of type number.
  • Base is an interface that extends Selectors, meaning it includes all the properties of the Selectors interface (in this case, the select function) and adds an event property of type Event.
  • Finally, declare var d3: D3.Base; is stating that there is a global variable named d3 (which is the standard way to access D3.js functions) and its type is D3.Base.

It’s important to note that these declarations do not implement any functionality themselves. They only describe the shape of existing JavaScript code (in this case, the D3.js library) to the TypeScript type-checker, which then allows you to write TypeScript code that interacts with D3.js with type checking and autocompletion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly