close

no-undef

Added in v0.3.3

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

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

Disallow the use of undeclared variables.

This rule reports identifiers that reference variables which have not been declared via var, let, const, function, class, import, or as a parameter.

Resolution follows ESLint scope semantics: bindings declared or imported in the current file, the standard language globals selected by languageOptions.ecmaVersion, and names declared through languageOptions.globals or a /* global */ comment. ecmaVersion defaults to "latest".

TypeScript's TypeChecker does not alter the result. DOM, Node, cross-file, and ambient .d.ts names are not implicit ESLint globals, even when TypeScript can resolve them. Declare host globals such as console, window, process, and setTimeout through languageOptions.globals or a /* global */ comment. TypeScript projects normally leave this core rule disabled because tsc already reports undeclared names.

For browser, Node.js, worker, and other runtime names, use the globals catalog exported by @rslint/core instead of listing every name manually. Scope each environment to the files where it exists; no runtime environment is enabled by default. See Configuring runtime globals for examples.

Options

typeof

Type: boolean Default: false

When set to true, typeof expressions will be checked for undeclared variables. By default, typeof of an undeclared variable does not trigger a warning, since typeof returns "undefined" for undeclared variables without throwing a ReferenceError.

Examples

Invalid

a = 1; // 'a' is not defined.
var x = b; // 'b' is not defined.
undeclaredFunc(); // 'undeclaredFunc' is not defined.

With { "typeof": true }:

typeof x === 'string'; // 'x' is not defined.

Valid

var a = 1;
a;

function f() {}
f();

typeof maybeUndefined === 'string';

Differences from ESLint

  • In TypeScript files, rslint applies languageOptions.ecmaVersion to runtime ECMAScript globals. ESLint with @typescript-eslint/parser uses that parser's default ESNext library instead. For example, with ecmaVersion: 2019, rslint reports BigInt(1) unless BigInt is configured as a global, while ESLint does not. With the default parser library, type-only ESNext names such as Record remain available in both.

Original Documentation