Skip to main content
Version: Latest (0.58.x)

Namespaces

Namespaces in TypeSpec allow you to group related types together. This organization makes your types easier to locate and helps avoid naming conflicts. Namespaces are merged across files, enabling you to reference any type from anywhere in your TypeSpec program using its namespace.

Basics

You can create a namespace using the namespace keyword.

namespace SampleNamespace {
model SampleModel {}
}

Note: The namespace name must be a valid TypeSpec identifier.

You can then use SampleNamespace from other locations:

model Foo {
sample: SampleNamespace.SampleModel;
}

Nested namespaces

Namespaces can contain sub-namespaces, offering additional layers of organization.

namespace Foo {
namespace Bar {
namespace Baz {
model SampleModel {}
}
}
}

Alternatively, you can simplify this using . notation:

namespace Foo.Bar.Baz {
model SampleModel {}
}

You can then use the sub-namespace from other locations using the fully qualified name.

model A {
sample: Foo.Bar.Baz.SampleModel;
}

File-level namespaces

You can define a namespace for all declarations within a file at the top of the file (after any import statements) using a blockless namespace statement:

namespace SampleNamespace;

model SampleModel {}

A file can only have one blockless namespace.

Using namespaces

You can expose the contents of a namespace to the current scope using the using keyword.

using SampleNamespace;

model Foo {
sample: SampleModel;
}

The bindings introduced by a using statement are local to the namespace in which they are declared. They do not become part of the namespace themselves.

namespace One {
model A {}
}

namespace Two {
using One;
alias B = A; // This is valid
}

alias C = Two.A; // This is not valid
alias C = Two.B; // This is valid