UNPKG

2.88 kBMarkdownView Raw
1# Disallow the use of custom TypeScript modules and namespaces (no-namespace)
2
3Custom TypeScript modules (`module foo {}`) and namespaces (`namespace foo {}`) are considered outdated
4ways to organize TypeScript code. ES2015 module syntax is now preferred (`import`/`export`).
5
6This rule still allows the use of TypeScript module declarations to describe external APIs (`declare module 'foo' {}`).
7
8## Rule Details
9
10This rule aims to standardise the way modules are declared.
11
12## Options
13
14This rule, in its default state, does not require any argument. If you would like to enable one
15or 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
18inside definition files (Default: `false`).
19
20Examples of **incorrect** code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": false }` options:
21```ts
22module foo {}
23namespace foo {}
24
25declare module foo {}
26declare namespace foo {}
27
28// anything inside a d.ts file
29```
30
31Examples of **correct** code for the default `{ "allowDeclarations": false, "allowDefinitionFiles": false }` options:
32```ts
33declare module 'foo' {}
34```
35
36### allowDeclarations
37Examples of **incorrect** code for the `{ "allowDeclarations": true }` option:
38```ts
39module foo {}
40namespace foo {}
41```
42
43Examples of **correct** code for the `{ "allowDeclarations": true }` option:
44```ts
45declare module 'foo' {}
46declare module foo {}
47declare namespace foo {}
48```
49
50Examples of **incorrect** code for the `{ "allowDeclarations": false }` option:
51```ts
52module foo {}
53namespace foo {}
54declare module foo {}
55declare namespace foo {}
56```
57
58Examples of **correct** code for the `{ "allowDeclarations": false }` option:
59```ts
60declare module 'foo' {}
61```
62
63### allowDefinitionFiles
64Examples of **incorrect** code for the `{ "allowDefinitionFiles": true }` option:
65```ts
66// if outside a d.ts file
67module foo {}
68namespace foo {}
69
70// if outside a d.ts file and allowDeclarations = false
71module foo {}
72namespace foo {}
73declare module foo {}
74declare namespace foo {}
75```
76
77Examples of **correct** code for the `{ "allowDefinitionFiles": true }` option:
78```ts
79declare module 'foo' {}
80
81// anything inside a d.ts file
82```
83
84## When Not To Use It
85
86If 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/)