no-undef
Added in v0.3.3Configuration
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
With { "typeof": true }:
Valid
Differences from ESLint
- In TypeScript files, rslint applies
languageOptions.ecmaVersionto runtime ECMAScript globals. ESLint with@typescript-eslint/parseruses that parser's default ESNext library instead. For example, withecmaVersion: 2019, rslint reportsBigInt(1)unlessBigIntis configured as a global, while ESLint does not. With the default parser library, type-only ESNext names such asRecordremain available in both.