ts-morph

Module / Namespace Declarations

Module declarations may look like any of the following:

namespace MyNamespace {
}

module MyModule {
}

declare module "some-module" {
}

declare module "other-module";

They can be retrieved from source files or other modules/namespaces:

const modules = sourceFile.getModules();
const namespace1 = sourceFile.getModule("Namespace1");
const firstNamespaceWithClass = sourceFile.getModule(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 retrieve namespaces from function bodies.

Add/Insert

Add or insert namespaces to a source file, namespace, or function like declarations by calling addModule(), addModules(), insertModule(), or insertModules().

const moduleDeclaration = sourceFile.addModule({
  name: "ModuleName",
});

Remove

Call .remove():

moduleDeclaration.remove();

Module, namespace, or global?

Check for the declaration kind or keywords:

moduleDeclaration.getDeclarationKind(); // returns: ModuleDeclarationKind
// or
moduleDeclaration.hasModuleKeyword(); // returns: boolean
moduleDeclaration.hasNamespaceKeyword(); // returns: boolean

Or set the declaration kind:

moduleDeclaration.setDeclarationKind(ModuleDeclarationKind.Namespace);
moduleDeclaration.setDeclarationKind(ModuleDeclarationKind.Module);
moduleDeclaration.setDeclarationKind(ModuleDeclarationKind.Global);

Or get the keyword:

// returns: the module or namespace keyword or undefined if global
moduleDeclaration.getDeclarationKindKeyword();

Reminder: Module declarations that are global have the following syntax:

declare module "my-library" {
  // this is a global namespace declaration
  global {
    const foo: string;
  }
}

Unwrap

A module declaration can be replaced with its body using the .unwrap() method.

Given the following code:

namespace MyNamespace {
  function someFunction() {
  }

  class SomeClass {
  }
}

Calling .unwrap() will change the code to the following:

function someFunction() {
}

class SomeClass {
}