close

valid-typeof

Configuration

PresetConfigured Value
✅ js.configs.recommended"error"
rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'valid-typeof': 'error',
    },
  },
]);

Rule Details

Enforces comparing typeof expressions against valid string literals. The typeof operator can only return one of the following strings: "undefined", "object", "boolean", "number", "string", "function", "symbol", "bigint". Comparing a typeof expression against any other value is almost certainly a bug.

Examples of incorrect code for this rule:

typeof foo === 'strnig';
typeof foo == 'undefned';
typeof bar != 'nunber';
typeof bar !== 'fucntion';
typeof foo === undefined;

Examples of correct code for this rule:

typeof foo === 'string';
typeof bar == 'undefined';
typeof baz === 'object';
typeof qux !== 'function';
typeof foo === typeof bar;

Options

requireStringLiterals

When set to true, requires that typeof expressions are only compared to string literals or other typeof expressions, and disallows comparisons to any other value.

Examples of additional incorrect code with { "requireStringLiterals": true }:

typeof foo === undefined;
typeof foo === Object;
typeof foo === someVariable;

Original Documentation