Rule: no-use-before-declare
Disallows usage of variables before their declaration.
This rule is primarily useful when using the var keyword since the compiler will
automatically detect if a block-scoped let and const variable is used before
declaration. Since most modern TypeScript doesn’t use var, this rule is generally
discouraged and is kept around for legacy purposes. It is slow to compute, is not
enabled in the built-in configuration presets, and should not be used to inform TSLint
design decisions.
Notes:
Config
Not configurable.
Config examples
"no-use-before-declare": true
Schema
null
Code examples:
Check that referenced variables are declared beforehand (default)
"rules": { "no-use-before-declare": true }
Passes
var hello = 'world';
var foo;
console.log(hello, foo, capitalize(hello));
// 'world', undefined, 'WORLD'
function capitalize(val) {
return val.toUpperCase();
}
import { default as foo1 } from "./lib";
import foo2 from "./lib";
import _, { map, foldl } from "./underscore";
import * as foo3 from "./lib";
import "./lib";
function declaredImports() {
console.log(foo1);
console.log(foo2);
console.log(foo3);
map([], (x) => x);
}
Fails
console.log(hello, foo);
var hello = 'world';
var foo;
function undeclaredImports() {
console.log(foo1);
console.log(foo2);
console.log(foo3);
map([], (x) => x);
}
import { default as foo1 } from "./lib";
import foo2 from "./lib";
import _, { map, foldl } from "./underscore";
import * as foo3 from "./lib";
import "./lib";