ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
84 lines (57 loc) • 1.86 kB
Markdown
title: Namespaces
## Namespaces
Namespaces (or modules) can be retrieved from source files or other namespaces:
```typescript
const namespaces = sourceFile.getNamespaces();
const namespace1 = sourceFile.getNamespace("Namespace1");
const firstNamespaceWithClass = sourceFile.getNamespace(n => n.getClasses().length > 0);
```
Most of the information you can get about namespaces is covered in other sections.
Note: Although it's a compile error, you can also retreive namespaces from function bodies.
### Add/Insert
Add or insert namespaces to a source file, namespace, or function like declarations by calling `addNamespace()`, `addNamespaces()`, `insertNamespace()`, or `insertNamespaces()`.
```typescript
const namespaceDeclaration = sourceFile.addNamespace({
name: "NamespaceName"
});
```
### Remove
Call `.remove()`:
```typescript
namespaceDeclaration.remove();
```
### Module or namespace?
Check for the keyword you want:
```typescript
namespaceDeclaration.hasModuleKeyword(); // returns: boolean
namespaceDeclaration.hasNamespaceKeyword(); // returns: boolean
```
Or set one or the other:
```typescript
namespaceDeclaration.setHasModuleKeyword(); // optionally pass in a boolean
namespaceDeclaration.setHasNamespaceKeyword();
```
Or get the keyword:
```typescript
namespaceDeclaration.getDeclarationTypeKeyword(); // returns: the module or namespace keyword
```
### Unwrap
A namespace declaration can be replaced with its body using the `.unwrap()` method.
Given the following code:
```typescript
namespace MyNamespace {
function someFunction() {
}
class SomeClass {
}
}
```
Calling `.unwrap()` on the namespace will change the code to the following:
```typescript
function someFunction() {
}
class SomeClass {
}
```