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.
Note: The namespace name must be a valid TypeSpec identifier.
You can then use SampleNamespace
from other locations:
Nested namespaces
Namespaces can contain sub-namespaces, offering additional layers of organization.
Alternatively, you can simplify this using .
notation:
You can then use the sub-namespace from other locations using the fully qualified name.
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:
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.
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.