no-use-before-define
Added in v0.8.1Configuration
Disallow the use of variables before they are defined.
Rule Details
In JavaScript, var declarations and function declarations are hoisted, so
using them before their declaration is legal but confusing. let, const, and
class declarations are not: reading them before the declaration throws at
runtime (the temporal dead zone). This rule reports a reference that appears
before the declaration it resolves to, and a reference that runs while that
declaration is still being initialized.
References to names that are not declared in the file — globals, ambient declarations, imports from other modules — are not reported.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Note that a reference from a different execution context still counts as
"before" when the declaration comes later in the file — the forward call in a
pair of mutually recursive functions is reported unless functions is turned
off:
Options
This rule takes one option, either the string "nofunc" or an object.
The string form "nofunc" is shorthand for { "functions": false }.
The object form supports the following properties.
functions
Whether references to function declarations are checked. Default: true.
Because function declarations are hoisted, calling one before its declaration is safe, so turning this off is common in codebases that define helpers at the bottom of a file.
Examples of correct code with { "functions": false }:
classes
Whether references to class declarations are checked. Default: true.
Turning it off only exempts references from a different execution context — a function body, a method, a non-static field initializer. A reference in the same context as the class definition is a temporal dead zone error and is still reported.
Examples of correct code with { "classes": false }:
Examples of incorrect code with { "classes": false }:
variables
Whether references to var, let, and const declarations are checked.
Default: true.
As with classes, turning it off only exempts references from a different
execution context.
Examples of correct code with { "variables": false }:
allowNamedExports
Whether the local names in export { ... } are exempt. Default: false.
Examples of correct code with { "allowNamedExports": true }:
enums
Whether references to TypeScript enum declarations are checked. Default:
true.
Examples of correct code with { "enums": false }:
typedefs
Whether references to TypeScript type aliases, interface declarations, and
generic type parameters are checked. Default: true.
Examples of correct code with { "typedefs": false }:
ignoreTypeReferences
Whether direct type references such as let x: Foo, and typeof Foo type
queries, are exempt. Default: true. As in ESLint core, this exemption does not
cover heritage names (implements Foo), qualified-name roots (NS.Foo),
export assignments, or type-predicate parameter names.
Examples of incorrect code with { "ignoreTypeReferences": false }: