Back to Biomejs

useValidTypeof

src/content/docs/linter/rules/use-valid-typeof.mdx

latest8.2 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/useValidTypeof`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`valid-typeof`](https://eslint.org/docs/latest/rules/valid-typeof)

How to configure

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

Description

This rule checks that the result of a typeof expression is compared to a valid value.

Examples

Invalid

js
typeof foo === "strnig";
<pre class="language-text"><code class="language-text">code-block.js:1:16 <a href="https://biomejs.dev/linter/rules/use-valid-typeof">lint/correctness/useValidTypeof</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">&quot;strnig&quot; is not a valid </span><span style="color: Tomato;"><strong>typeof</strong></span><span style="color: Tomato;"> value.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>typeof foo === &quot;strnig&quot;; <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>2 │ </strong> </code></pre>
js
typeof foo == "undefimed";
<pre class="language-text"><code class="language-text">code-block.js:1:15 <a href="https://biomejs.dev/linter/rules/use-valid-typeof">lint/correctness/useValidTypeof</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">&quot;undefimed&quot; is not a valid </span><span style="color: Tomato;"><strong>typeof</strong></span><span style="color: Tomato;"> value.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>typeof foo == &quot;undefimed&quot;; <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>2 │ </strong> </code></pre>
js
typeof bar != "nunber";
<pre class="language-text"><code class="language-text">code-block.js:1:15 <a href="https://biomejs.dev/linter/rules/use-valid-typeof">lint/correctness/useValidTypeof</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">&quot;nunber&quot; is not a valid </span><span style="color: Tomato;"><strong>typeof</strong></span><span style="color: Tomato;"> value.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>typeof bar != &quot;nunber&quot;; <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>2 │ </strong> </code></pre>
js
typeof foo === undefined;
<pre class="language-text"><code class="language-text">code-block.js:1:16 <a href="https://biomejs.dev/linter/rules/use-valid-typeof">lint/correctness/useValidTypeof</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Invalid </span><span style="color: Tomato;"><strong>typeof</strong></span><span style="color: Tomato;"> comparison.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>typeof foo === undefined; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Compare with one of the following string literals:</span> - &quot;bigint&quot; - &quot;boolean&quot; - &quot;function&quot; - &quot;number&quot; - &quot;object&quot; - &quot;string&quot; - &quot;symbol&quot; - &quot;undefined&quot; </code></pre>
js
typeof foo == 0;
<pre class="language-text"><code class="language-text">code-block.js:1:15 <a href="https://biomejs.dev/linter/rules/use-valid-typeof">lint/correctness/useValidTypeof</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Invalid </span><span style="color: Tomato;"><strong>typeof</strong></span><span style="color: Tomato;"> comparison.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>typeof foo == 0; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Compare with one of the following string literals:</span> - &quot;bigint&quot; - &quot;boolean&quot; - &quot;function&quot; - &quot;number&quot; - &quot;object&quot; - &quot;string&quot; - &quot;symbol&quot; - &quot;undefined&quot; </code></pre>

Valid

js
typeof foo === "string";
js
typeof bar == "undefined";
js
typeof bar === typeof qux;
js
typeof foo === bar
</TabItem> </Tabs>