The usage of ES2015 module syntax is favored over custom TypeScript modules and namespaces as per the rule @typescript-eslint/no-namespace. So, it is advised to rewrite the sentence accordingly in any context if necessary.

46πŸ‘

βœ…

This is a lint error caused by the no-namespace lint rule.

If you find the rule helpful and want to keep it, you will need to modify your code by replacing namespaces with imports and exports. The documentation of the rule provides details on what changes are needed for a fix.

If you like the rule but want to disable it for this specific line, you can add the following comment just above it:

// eslint-disable-next-line @typescript-eslint/no-namespace

If you dislike the rule and wish to disable it entirely, you can edit your .eslintrc file and add the following line:

rules: {
  "@typescript-eslint/no-namespace": "off"
}

41πŸ‘

Fixing the Error in TypeScript

To fix this error, instead of:

export namespace InternalThings {
    export function myFunction() {
    }

    export class MyClass {
    }
}
import { InternalThings } from './internal-things';

InternalThings.myFunction();

you can make all the members of the namespace directly accessible:

export function myFunction() {
}

export class MyClass {
}

Now, you can import it using the following syntax:

import * as InternalThings from './internal-things';

InternalThings.myFunction();

The main advantage of this approach is that users of your module can choose to import only what they need or give your module a custom name:

import * as CustomModuleName from './internal-things';

CustomModuleName.myFunction();
import { MyClass } from './internal-things';

let field = new MyClass();

2πŸ‘

The error is being reported by eslint. To resolve this issue, you can either exclude the β€˜@typescript-eslint/no-namespace’ rule in the eslint configuration or update your code to use ES6 syntax.

The usage of custom TypeScript modules (module foo {}) and namespaces (namespace
foo {}) is considered outdated in organizing TypeScript code. The preferred method now is to use ES2015 module syntax (import/export).

Please refer to the following link for more information: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-namespace.md

Read more interesting post

Leave a comment