id | title |
---|---|
namespaces |
Namespaces |
Namespaces let you group related types together into namespaces. This helps organize your types, making them easier to find and prevents name conflicts. Namespaces are merged across files, so you can reference any type anywhere in your TypeSpec program via its namespace.
Create a namespace with the namespace
keyword.
namespace SampleNamespace {
model SampleModel {}
}
The name of a namespace must be a valid TypeSpec identifier.
The SampleNamespace
can then be used from other places:
model Foo {
sample: SampleNamespace.SampleModel;
}
Namespaces can contain sub namespaces providing additional granularity
namespace Foo {
namespace Bar {
namespace Baz {
model SampleModel {}
}
}
}
or this can be simplified using .
notation
namespace Foo.Bar.Baz {
model SampleModel {}
}
The sub-namespace can then be used from other places using the fully qualified name.
model A {
sample: Foo.Bar.Baz.SampleModel;
}
A namespace for all declarations contained in a file can be provided at the top (After the import
statements) using a blockless namespace statement
namespace SampleNamespace;
model SampleModel {}
A file can only have a single blockless namespace.
The content of a namespace can be exposed 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 they are declared in. They do not become part of the namespace themselves.
namespace One {
model A {}
}
namespace Two {
using One;
alias B = A; // ok
}
alias C = One.A; // not ok
alias C = Two.B; // ok