Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES2015 module syntax is preferred over custom TypeScript modules and namespaces @typescript-eslint/no-namespace

I'm receiving the following error running npm start:

ES2015 module syntax is preferred over custom TypeScript modules and namespaces @typescript-eslint/no-namespace

    namespace InternalThings {...} 

I tried to research this but it's very confusing.

Why does this is happening? How to fix it?

I tried to put some flags on my tsconfig.json but so far no success;

like image 895
Gabriel Marcondes Avatar asked Oct 07 '19 13:10

Gabriel Marcondes


People also ask

Does ES2015 support namespace?

ES2015 module syntax is preferred over custom TypeScript modules and namespaces @typescript-eslint/no-namespace.

Which is the default module format used while compiling modules in TypeScript?

By default, files in TypeScript are treated as global scripts. This means that any variable, class, function, or other construct declared in the file is available globally. As soon as you start using modules in a file, this file becomes module-scoped, and is no longer executed globally.

What is declare module in TypeScript?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...


2 Answers

This is a lint error, caused by this lint rule: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-namespace.md

If you find the rule useful and want to keep it, then you'll need to modify your code to use import and export instead of namespace. See the documentation of the rule for what counts as a fix.

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

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

If you don't like the rule and want to disable it entirely, then edit your .eslintrc file to have the following line:

rules: {   "@typescript-eslint/no-namespace": "off" } 
like image 90
Nicholas Tower Avatar answered Oct 08 '22 09:10

Nicholas Tower


To fix this error, instead of:

export namespace InternalThings {     export function myFunction() {     }      export class MyClass {     } } 
import { InternalThings } from './internal-things';  InternalThings.myFunction(); 

you expose all the members of the namespace directly:

export function myFunction() { }  export class MyClass { } 

and you import it like this:

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

The main idea is that users of your module can import only what they want, or name your module differently:

import * as CustomModuleName from './internal-things';  CustomModuleName.myFunction(); 
import { MyClass } from './internal-things';  let field = new MyClass(); 
like image 43
Métoule Avatar answered Oct 08 '22 08:10

Métoule