1 | # Disallow the use of custom TypeScript modules and namespaces (no-namespace)
|
2 |
|
3 | Custom TypeScript modules (`module foo {}`) and namespaces (`namespace foo {}`) are considered outdated
|
4 | ways to organize TypeScript code. ES2015 module syntax is now preferred (`import`/`export`).
|
5 |
|
6 | This rule still allows the use of TypeScript module declarations to describe external APIs (`declare module 'foo' {}`).
|
7 |
|
8 | ## Rule Details
|
9 |
|
10 | This rule aims to standardise the way modules are declared.
|
11 |
|
12 | ## Options
|
13 |
|
14 | This rule, in its default state, does not require any argument. If you would like to enable one
|
15 | or more of the following you may pass an object with the options set as follows:
|
16 | - `allowDeclarations` set to `true` will allow you to `declare` custom TypeScript modules and namespaces (Default: `false`).
|
17 | - `allowDefinitionFiles` set to `true` will allow you to `declare` and use custom TypeScript modules and namespaces
|
18 | inside definition files (Default: `false`).
|
19 |
|
20 | Examples of **incorrect** code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": false }` options:
|
21 | ```ts
|
22 | module foo {}
|
23 | namespace foo {}
|
24 |
|
25 | declare module foo {}
|
26 | declare namespace foo {}
|
27 |
|
28 | // anything inside a d.ts file
|
29 | ```
|
30 |
|
31 | Examples of **correct** code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": false }` options:
|
32 | ```ts
|
33 | declare module 'foo' {}
|
34 | ```
|
35 |
|
36 | ### allowDeclarations
|
37 | Examples of **incorrect** code for the `{ "allowDeclarations": true }` option:
|
38 | ```ts
|
39 | module foo {}
|
40 | namespace foo {}
|
41 | ```
|
42 |
|
43 | Examples of **correct** code for the `{ "allowDeclarations": true }` option:
|
44 | ```ts
|
45 | declare module 'foo' {}
|
46 | declare module foo {}
|
47 | declare namespace foo {}
|
48 | ```
|
49 |
|
50 | Examples of **incorrect** code for the `{ "allowDeclarations": false }` option:
|
51 | ```ts
|
52 | module foo {}
|
53 | namespace foo {}
|
54 | declare module foo {}
|
55 | declare namespace foo {}
|
56 | ```
|
57 |
|
58 | Examples of **correct** code for the `{ "allowDeclarations": false }` option:
|
59 | ```ts
|
60 | declare module 'foo' {}
|
61 | ```
|
62 |
|
63 | ### allowDefinitionFiles
|
64 | Examples of **incorrect** code for the `{ "allowDefinitionFiles": true }` option:
|
65 | ```ts
|
66 | // if outside a d.ts file
|
67 | module foo {}
|
68 | namespace foo {}
|
69 |
|
70 | // if outside a d.ts file and allowDeclarations = false
|
71 | module foo {}
|
72 | namespace foo {}
|
73 | declare module foo {}
|
74 | declare namespace foo {}
|
75 | ```
|
76 |
|
77 | Examples of **correct** code for the `{ "allowDefinitionFiles": true }` option:
|
78 | ```ts
|
79 | declare module 'foo' {}
|
80 |
|
81 | // anything inside a d.ts file
|
82 | ```
|
83 |
|
84 | ## When Not To Use It
|
85 |
|
86 | If you are using the ES2015 module syntax, then you will not need this rule.
|
87 |
|
88 | ## Further Reading
|
89 |
|
90 | * [Modules](https://www.typescriptlang.org/docs/handbook/modules.html)
|
91 | * [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html)
|
92 | * [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html)
|
93 |
|
94 | ## Compatibility
|
95 |
|
96 | * TSLint: [no-namespace](https://palantir.github.io/tslint/rules/no-namespace/)
|