8000 docs: add global variable rule disabling FAQ for ESLint (#9865) · abrahamguo/typescript-eslint@2421575 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2421575

Browse files
docs: add global variable rule disabling FAQ for ESLint (typescript-eslint#9865)
* docs: add global variable rule disabling FAQ for ESLint * Linked to FAQ too, good idea * disable-next-line
1 parent d9f66da commit 2421575

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

docs/troubleshooting/faqs/ESLint.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,51 @@ module.exports = {
8484

8585
If you choose to leave on the ESLint `no-undef` lint rule, you can [manually define the set of allowed `globals` in your ESLint config](https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals), and/or you can use one of the [pre-defined environment (`env`) configurations](https://eslint.org/docs/user-guide/configuring/language-options#specifying-environments).
8686

87+
## I get errors from the `@typescript-eslint/no-namespace` and/or `no-var` rules about declaring global variables
88+
89+
Two common solutions in TypeScript for declaring the existence of a global variable include:
90+
91+
- `declare global` with a `var`, which violates [`no-var`](https://eslint.org/docs/latest/rules/no-var):
92+
93+
```ts
94+
declare global {
95+
var myValue: string;
96+
// Unexpected var, use let or const instead. eslint (no-var)
97+
}
98+
99+
myValue;
100+
```
101+
102+
- `declare namespace globalThis`, which violates [`@typescript-eslint/no-namespace`](https://typescript-eslint.io/rules/no-namespace):
103+
104+
```ts
105+
declare namespace globalThis {
106+
// ES2015 module syntax is preferred over namespaces. eslint (@typescript-eslint/no-namespace)
107+
let myValue: string;
108+
}
109+
110+
globalThis.myValue;
111+
```
112+
113+
[Using global variables is generally discouraged](https://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice).
114+
If possible, it's best to avoid declaring globals altogether.
115+
116+
If you absolutely must use one of the two strategies, then you can use an [ESLint configuration comment](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) to disable rules as needed.
117+
For example:
118+
119+
```ts
120+
declare global {
121+
// eslint-disable-next-line no-var -- Provided by an old third-party integration.
122+
var myValue: string;
123+
}
124+
```
125+
126+
:::tip
127+
Whenever you need to disable an ESLint rule, it's best to include an informative comment explaining why.
128+
:::
129+
130+
See [#9582](https://github.com/typescript-eslint/typescript-eslint/issues/9582 'typescript-eslint/typescript-eslint#9582 Docs: globalThis without ignores of no-var and no-namespace') and [#7941](https://github.com/typescript-eslint/typescript-eslint/issues/7941 'typescript-eslint/typescript-eslint#7941 Base rule extension: no-var configuration for declarations') for discussions around typescript-eslint supporting these use cases.
131+
87132
## Can I use ESLint's `--cache` with typescript-eslint?
88133

89134
[ESLint's `--cache` option](https://eslint.org/docs/latest/use/command-line-interface#caching) caches on a per-file basis.

packages/eslint-plugin/docs/rules/no-namespace.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
137137

138138
## Further Reading
139139

140+
{/* cspell:disable-next-line */}
141+
142+
- [FAQ: I get errors from the `@typescript-eslint/no-namespace` and/or `no-var` rules about declaring global variables](/troubleshooting/faqs/eslint#i-get-errors-from-the-typescript-eslintno-namespace-andor-no-var-rules-about-declaring-global-variables)
140143
- [Modules](https://www.typescriptlang.org/docs/handbook/modules.html)
141144
- [Namespaces](https://www.typescriptlang.org/docs/handbook/namespaces.html)
142145
- [Namespaces and Modules](https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html)

0 commit comments

Comments
 (0)
0