@typescript-eslint/init-declarations
Require or disallow a value in variable declarations
Also called typescript-eslint/init-declarations, typescript/init-declarations
A variable declared without a value is undefined until a later line assigns it. If one branch skips the assignment, the code after it reads undefined. "always" requires a value in every declaration. "never" requires the opposite style: declare first, assign later.
It's the TypeScript version of init-declarations. A declare let and the variables in a declare namespace can't have a value, so "always" doesn't report them.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/init-declarations" = "error" # or "warn"Every option, set to its default:
[lint.rules]
# "always" or "never"
"@typescript-eslint/init-declarations" = ["error", "always"]Examples#
"always"1 problemlet discountVariable discount should be initialized on declaration.: number;
if (isMember) {
discount = 10;
}"always"No problemslet discount = 0;
if (isMember) {
discount = 10;
}
declare let isMember: boolean;Options#
[lint.rules]
"@typescript-eslint/init-declarations" = ["error", "always"]| Option | What it does |
|---|---|
"always" | Report a var or let that has no value. |
"never" | Report a var or let that has a value. const and using need one, so they're allowed. |
The default is "always":
let discountVariable discount should be initialized on declaration.: number;With "never":
"never"1 problemlet discount = 0Variable discount should not be initialized on declaration.;"never"No problemslet discount: number;
const rate = 0.2;
discount = 100 * rate;With "never", a second option { "ignoreForLoopInit": true } allows the variable in the head of a for loop. It's false by default.
"never"1 problemfor (let i = 0Variable i should not be initialized on declaration.; i < 3; i++) {
console.log(i);
}"never", { "ignoreForLoopInit": true }No problemsfor (let i = 0; i < 3; i++) {
console.log(i);
}