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 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",


Call .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:


Or get the keyword:

// returns: the module or namespace keyword or undefined if global

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

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


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 {