TsSimpleAst

Namespaces

Namespaces (or modules) can be retrieved from source files or other namespaces:

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.

Module or namespace?

Check for the keyword you want:

namespaceDeclaration.hasModuleKeyword(); // returns: boolean
namespaceDeclaration.hasNamespaceKeyword(); // returns: boolean

Or set one or the other:

namespaceDeclaration.setHasModuleKeyword(); // optionally pass in a boolean
namespaceDeclaration.setHasNamespaceKeyword();

Or get the keyword:

namespaceDeclaration.getDeclarationTypeKeyword(); // returns: the module or namespace keyword