UNPKG

2.9 kBMarkdownView Raw
1# `no-namespace`
2
3Disallow the use of custom TypeScript modules and namespaces.
4
5Custom TypeScript modules (`module foo {}`) and namespaces (`namespace foo {}`) are considered outdated
6ways to organize TypeScript code. ES2015 module syntax is now preferred (`import`/`export`).
7
8This rule still allows the use of TypeScript module declarations to describe external APIs (`declare module 'foo' {}`).
9
10## Rule Details
11
12This rule aims to standardize the way modules are declared.
13
14## Options
15
16This rule, in its default state, does not require any argument. If you would like to enable one
17or 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
23Examples of code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": true }` options:
24
25<!--tabs-->
26
27### ❌ Incorrect
28
29```ts
30module foo {}
31namespace foo {}
32
33declare module foo {}
34declare namespace foo {}
35```
36
37### ✅ Correct
38
39```ts
40declare module 'foo' {}
41
42// anything inside a d.ts file
43```
44
45<!--/tabs-->
46
47### `allowDeclarations`
48
49Examples of code for the `{ "allowDeclarations": true }` option:
50
51<!--tabs-->
52
53#### ❌ Incorrect
54
55```ts
56module foo {}
57namespace foo {}
58```
59
60#### ✅ Correct
61
62```ts
63declare module 'foo' {}
64declare module foo {}
65declare namespace foo {}
66
67declare global {
68 namespace foo {}
69}
70
71declare module foo {
72 namespace foo {}
73}
74```
75
76<!--/tabs-->
77
78Examples of code for the `{ "allowDeclarations": false }` option:
79
80<!--tabs-->
81
82#### ❌ Incorrect
83
84```ts
85module foo {}
86namespace foo {}
87declare module foo {}
88declare namespace foo {}
89```
90
91#### ✅ Correct
92
93```ts
94declare module 'foo' {}
95```
96
97### `allowDefinitionFiles`
98
99Examples of code for the `{ "allowDefinitionFiles": true }` option:
100
101<!--tabs-->
102
103#### ❌ Incorrect
104
105```ts
106// if outside a d.ts file
107module foo {}
108namespace foo {}
109
110// if outside a d.ts file and allowDeclarations = false
111module foo {}
112namespace foo {}
113declare module foo {}
114declare namespace foo {}
115```
116
117#### ✅ Correct
118
119```ts
120declare module 'foo' {}
121
122// anything inside a d.ts file
123```
124
125## When Not To Use It
126
127If 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