I recently got a legacy .ts-file and want to update it.
Two of the warnings say:
'namespace' and 'module' are disallowed
and
The internal 'module' syntax is deprecated, use the 'namespace' keyword instead.
I read about using standardized ES6-style external modules, but I cannot figure out, how to do this.
My code looks like the following:
export namespace MySpace
{
export module MyModule
{
export interface MyInterface
{
}
export class MyClass
{
}
...
}
}
May anyone give me a hint, how to update this structure to actual style rules?
thank you all a lot in advance!
best regards
mrt
A module is a way which is used to organize the code in separate files and can execute in their local scope, not in the global scope. A namespace is a way which is used for logical grouping of functionalities with local scoping.
To use it, it must be included using triple slash reference syntax e.g. ///<reference path="path to namespace file" /> . Must import it first in order to use it elsewhere. Compile using the --outFile command. Compile using the --module command.
The namespace is a way which is used for logical grouping of functionalities. It encapsulates the features and objects that share common relationships. It allows us to organize our code in a much cleaner way. A namespace is also known as internal modules.
Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .
The internal 'module' syntax is deprecated, use the 'namespace' keyword instead.
This warning from the linter is about export module MyModule
, because MyModule
is not a module but a namespace. The keyword that should be used is namespace
: export namespace MyModule
.
'namespace' and 'module' are disallowed
This warning from the linter is about export namespace MySpace
. A top-level export
implies the file is a module, and it is a bad practice to use namespaces and modules together.
May anyone give me a hint, how to update this structure to actual style rules?
Do not use namespace
at all. Here is your code in a module:
export interface MyInterface
{
}
export class MyClass
{
}
// …
See also: an introduction to modules from Mozilla.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With