@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:

bunfig.toml
[lint.rules]
"@typescript-eslint/init-declarations" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
# "always" or "never"
"@typescript-eslint/init-declarations" = ["error", "always"]

Examples#

example.tswith "always"1 problem
let discount: number;

if (isMember) {
  discount = 10;
}
example.tswith "always"No problems
let discount = 0;

if (isMember) {
  discount = 10;
}

declare let isMember: boolean;

Options#

bunfig.toml
[lint.rules]
"@typescript-eslint/init-declarations" = ["error", "always"]
OptionWhat 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":

example.ts1 problem
let discount: number;

With "never":

example.tswith "never"1 problem
let discount = 0;
example.tswith "never"No problems
let 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.

example.tswith "never"1 problem
for (let i = 0; i < 3; i++) {
  console.log(i);
}
example.tswith "never", { "ignoreForLoopInit": true }No problems
for (let i = 0; i < 3; i++) {
  console.log(i);
}