1 | # `no-namespace`
|
2 |
|
3 | Disallow the use of custom TypeScript modules and namespaces.
|
4 |
|
5 | Custom TypeScript modules (`module foo {}`) and namespaces (`namespace foo {}`) are considered outdated
|
6 | ways to organize TypeScript code. ES2015 module syntax is now preferred (`import`/`export`).
|
7 |
|
8 | This rule still allows the use of TypeScript module declarations to describe external APIs (`declare module 'foo' {}`).
|
9 |
|
10 | ## Rule Details
|
11 |
|
12 | This rule aims to standardize the way modules are declared.
|
13 |
|
14 | ## Options
|
15 |
|
16 | This rule, in its default state, does not require any argument. If you would like to enable one
|
17 | or more of the following you may pass an object with the options set as follows:
|
18 |
|
19 | - `allowDeclarations` set to `true` will allow you to `declare` custom TypeScript modules and namespaces (Default: `false`).
|
20 | - `allowDefinitionFiles` set to `true` will allow you to `declare` and use custom TypeScript modules and namespaces
|
21 | inside definition files (Default: `true`).
|
22 |
|
23 | Examples of code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": true }` options:
|
24 |
|
25 |
|
26 |
|
27 | ### ❌ Incorrect
|
28 |
|
29 | ```ts
|
30 | module foo {}
|
31 | namespace foo {}
|
32 |
|
33 | declare module foo {}
|
34 | declare namespace foo {}
|
35 | ```
|
36 |
|
37 | ### ✅ Correct
|
38 |
|
39 | ```ts
|
40 | declare module 'foo' {}
|
41 |
|
42 | // anything inside a d.ts file
|
43 | ```
|
44 |
|
45 |
|
46 |
|
47 | ### `allowDeclarations`
|
48 |
|
49 | Examples of code for the `{ "allowDeclarations": true }` option:
|
50 |
|
51 |
|
52 |
|
53 | #### ❌ Incorrect
|
54 |
|
55 | ```ts
|
56 | module foo {}
|
57 | namespace foo {}
|
58 | ```
|
59 |
|
60 | #### ✅ Correct
|
61 |
|
62 | ```ts
|
63 | declare module 'foo' {}
|
64 | declare module foo {}
|
65 | declare namespace foo {}
|
66 |
|
67 | declare global {
|
68 | namespace foo {}
|
69 | }
|
70 |
|
71 | declare module foo {
|
72 | namespace foo {}
|
73 | }
|
74 | ```
|
75 |
|
76 |
|
77 |
|
78 | Examples of code for the `{ "allowDeclarations": false }` option:
|
79 |
|
80 |
|
81 |
|
82 | #### ❌ Incorrect
|
83 |
|
84 | ```ts
|
85 | module foo {}
|
86 | namespace foo {}
|
87 | declare module foo {}
|
88 | declare namespace foo {}
|
89 | ```
|
90 |
|
91 | #### ✅ Correct
|
92 |
|
93 | ```ts
|
94 | declare module 'foo' {}
|
95 | ```
|
96 |
|
97 | ### `allowDefinitionFiles`
|
98 |
|
99 | Examples of code for the `{ "allowDefinitionFiles": true }` option:
|
100 |
|
101 |
|
102 |
|
103 | #### ❌ Incorrect
|
104 |
|
105 | ```ts
|
106 | // if outside a d.ts file
|
107 | module foo {}
|
108 | namespace foo {}
|
109 |
|
110 | // if outside a d.ts file and allowDeclarations = false
|
111 | module foo {}
|
112 | namespace foo {}
|
113 | declare module foo {}
|
114 | declare namespace foo {}
|
115 | ```
|
116 |
|
117 | #### ✅ Correct
|
118 |
|
119 | ```ts
|
120 | declare module 'foo' {}
|
121 |
|
122 | // anything inside a d.ts file
|
123 | ```
|
124 |
|
125 | ## When Not To Use It
|
126 |
|
127 | If you are using the ES2015 module syntax, then you will not need this rule.
|
128 |
|
129 | ## Further Reading
|
130 |
|
131 | - [Modules](https://www.typescriptlang.org/docs/handbook/modules.html)
|
132 | - [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html)
|
133 | - [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html)
|
134 |
|
135 | ## Related To
|
136 |
|
137 | - TSLint: [no-namespace](https://palantir.github.io/tslint/rules/no-namespace/)
|
138 |
|
139 | ## Attributes
|
140 |
|
141 | - Configs:
|
142 | - [x] ✅ Recommended
|
143 | - [x] 🔒 Strict
|
144 | - [ ] 🔧 Fixable
|
145 | - [ ] 💭 Requires type information
|