tslint: namespace and module are disallowed
Asked Answered
A

1

12

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

Afrikander answered 9/5, 2019 at 12:22 Comment(2)
palantir.github.io/tslint/rules/no-namespaceLavoie
thank you, I've found this site already, but I do not know how to interprete it. I'm pretty new to js/ts at all, sorry bout thatAfrikander
I
9

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.

Immoral answered 9/5, 2019 at 13:59 Comment(6)
Namespaces are required to add 'static' members to interfaces. Is there another syntax to use, or do I just need to disable this tslint rule?Separate
@AndrewArnott I'm unsure what is a "static" member. Maybe it's related to a class? Maybe a simple declare class would be enough?Immoral
Oh interesting. Yes it seems I can export interface A and export class A in the same module, defining static methods (and maybe others?) such that those methods appear on A.SomeMethod all while A remains to be treated like an interface type by the TypeScript compiler. More testing to do, but this is hopeful.Separate
@Immoral Why are you suggesting not to use namespaces? just curiousMercia
@Mercia Because TypeScript is JavaScript with syntax for types… except for namespace and enum, which don't exist in JS and are not types. IMO these two features should be avoided. Prefer JS modules over namespaces, and unions over enums.Immoral
@Immoral Enums are a critical infrastructure man ... but thank you for the perspective!Mercia

© 2022 - 2024 — McMap. All rights reserved.