Back to Biomejs

noUndeclaredVariables

src/content/docs/linter/rules/no-undeclared-variables.mdx

latest6.5 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/noUndeclaredVariables`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-undef`](https://eslint.org/docs/latest/rules/no-undef)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"noUndeclaredVariables": "error"
			}
		}
	}
}

Description

Prevents the usage of variables that haven't been declared inside the document.

If you need to allow-list some global bindings, you can use the javascript.globals configuration.

Examples

Invalid

js
foobar;
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-undeclared-variables">lint/correctness/noUndeclaredVariables</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">The </span><span style="color: Tomato;"><strong>foobar</strong></span><span style="color: Tomato;"> variable is undeclared.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>foobar; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">By default, Biome recognizes browser and Node.js globals. </span> <span style="color: lightgreen;">You can ignore more globals using the </span><span style="color: lightgreen;"><a href="https://biomejs.dev/reference/configuration/#javascriptglobals">javascript.globals</a></span><span style="color: lightgreen;"> configuration.</span> </code></pre>
js
// throw diagnostic for JavaScript files
PromiseLike;
<pre class="language-text"><code class="language-text">code-block.js:2:1 <a href="https://biomejs.dev/linter/rules/no-undeclared-variables">lint/correctness/noUndeclaredVariables</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">The </span><span style="color: Tomato;"><strong>PromiseLike</strong></span><span style="color: Tomato;"> variable is undeclared.</span> <strong>1 │ </strong>// throw diagnostic for JavaScript files <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>PromiseLike; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">By default, Biome recognizes browser and Node.js globals. </span> <span style="color: lightgreen;">You can ignore more globals using the </span><span style="color: lightgreen;"><a href="https://biomejs.dev/reference/configuration/#javascriptglobals">javascript.globals</a></span><span style="color: lightgreen;"> configuration.</span> </code></pre>

Valid

ts
type B<T> = PromiseLike<T>

Options

checkTypes

When set to true, it checks for undeclared types too. The option defaults to false.

json
{
	"linter": {
		"rules": {
			"correctness": {
				"noUndeclaredVariables": {
					"options": {
						"checkTypes": true
					}
				}
			}
		}
	}
}

ts
type A = number extends infer T ? never : T;
<pre class="language-text"><code class="language-text">code-block.ts:1:43 <a href="https://biomejs.dev/linter/rules/no-undeclared-variables">lint/correctness/noUndeclaredVariables</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">The </span><span style="color: Tomato;"><strong>T</strong></span><span style="color: Tomato;"> variable is undeclared.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>type A = number extends infer T ? never : T; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">By default, Biome recognizes browser and Node.js globals. </span> <span style="color: lightgreen;">You can ignore more globals using the </span><span style="color: lightgreen;"><a href="https://biomejs.dev/reference/configuration/#javascriptglobals">javascript.globals</a></span><span style="color: lightgreen;"> configuration.</span> </code></pre> </TabItem> </Tabs>