You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/troubleshooting/faqs/ESLint.mdx
+45Lines changed: 45 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -84,6 +84,51 @@ module.exports = {
84
84
85
85
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).
86
86
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
+
declareglobal {
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
+
declarenamespaceglobalThis {
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
+
declareglobal {
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
+
87
132
## Can I use ESLint's `--cache` with typescript-eslint?
88
133
89
134
[ESLint's `--cache` option](https://eslint.org/docs/latest/use/command-line-interface#caching) caches on a per-file basis.
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/no-namespace.mdx
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -137,6 +137,9 @@ You might consider using [ESLint disable comments](https://eslint.org/docs/lates
137
137
138
138
## Further Reading
139
139
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)
0 commit comments