Index

init-declarations

要求或禁止在变量声明中进行初始化

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

在 JavaScript 中,变量可以在声明时赋值,或者在之后的任何时候使用赋值语句赋值。例如,在以下代码中,foo 在声明时被初始化,而 bar 则是在之后被初始化。

🌐 In JavaScript, variables can be assigned during declaration, or at any point afterwards using an assignment statement. For example, in the following code, foo is initialized during declaration, while bar is initialized later.

let foo = 1;
let bar;

if (foo) {
    bar = 1;
} else {
    bar = 2;
}

规则详情

🌐 Rule Details

这条规则旨在在声明时强制执行或消除变量初始化。例如,在下面的代码中,foo 在声明时被初始化,而 bar 则没有。

🌐 This rule is aimed at enforcing or eliminating variable initializations during declaration. For example, in the following code, foo is initialized during declaration, while bar is not.

let foo = 1;
let bar;

bar = 2;

该规则旨在为变量初始化和声明带来一致性。

🌐 This rule aims to bring consistency to variable initializations and declarations.

选项

🌐 Options

该规则有两个选项:

🌐 The rule takes two options:

  1. 一个字符串,必须是 "always"(默认值),以强制在声明时初始化,或 "never" 以禁止在声明时初始化。此规则适用于 varletconstusingawait using 变量,但对于 constusingawait using 变量,"never" 会被忽略,因为不初始化这些变量会生成解析错误。
  2. 进一步控制此规则行为的对象。目前,唯一可用的参数是 ignoreForLoopInit,它表示在设置 "never" 时,是否允许在 for 循环中声明时初始化,因为这是一个非常典型的用例。

你可以按如下方式配置规则:

🌐 You can configure the rule as follows:

变量必须在声明时初始化(默认)

🌐 Variables must be initialized at declaration (default)

{
    "init-declarations": ["error", "always"],
}

变量不能在声明时初始化

🌐 Variables must not be initialized at declaration

{
    "init-declarations": ["error", "never"]
}

变量不得在声明时初始化,除非在允许的 for 循环中

🌐 Variables must not be initialized at declaration, except in for loops, where it is allowed

{
    "init-declarations": ["error", "never", { "ignoreForLoopInit": true }]
}

always

默认 "always" 选项的错误代码示例:

🌐 Examples of incorrect code for the default "always" option:

在线运行
/*eslint init-declarations: ["error", "always"]*/

function foo() {
    var bar;
    let baz;
}

默认 "always" 选项的正确代码示例:

🌐 Examples of correct code for the default "always" option:

在线运行
/*eslint init-declarations: ["error", "always"]*/

function foo() {
    var bar = 1;
    let baz = 2;
    const qux = 3;
	using quux = getSomething();
}

async function foobar() {
	await using quux = getSomething();
}

never

针对 "never" 选项的错误代码示例:

🌐 Examples of incorrect code for the "never" option:

在线运行
/*eslint init-declarations: ["error", "never"]*/

function foo() {
    var bar = 1;
    let baz = 2;

    for (let i = 0; i < 1; i++) {}
}

适用于 "never" 选项的正确代码示例:

🌐 Examples of correct code for the "never" option:

在线运行
/*eslint init-declarations: ["error", "never"]*/

function foo() {
    var bar;
    let baz;
    const buzz = 1;
	using quux = getSomething();
}

async function foobar() {
	await using quux = getSomething();
}

"never" 选项会忽略 constusingawait using 变量的初始化。

🌐 The "never" option ignores const, using, and await using variable initializations.

ignoreForLoopInit

适用于 "never", { "ignoreForLoopInit": true } 选项的正确代码示例:

🌐 Examples of correct code for the "never", { "ignoreForLoopInit": true } options:

在线运行
/*eslint init-declarations: ["error", "never", { "ignoreForLoopInit": true }]*/
for (let i = 0; i < 1; i++) {}

此规则还支持 TypeScript 类型语法。

🌐 This rule additionally supports TypeScript type syntax.

针对该规则的 错误 TypeScript 代码示例:

🌐 Examples of incorrect TypeScript code for this rule:

在线运行
/* eslint init-declarations: ["error", "never"] */

let arr: string[] = ['arr', 'ar'];

const class1 = class NAME {
	constructor() {
	  var name1: string = 'hello';
	}
};

namespace myLib {
	let numberOfGreetings: number = 2;
}

在线运行
/* eslint init-declarations: ["error", "always"] */

namespace myLib {
	let numberOfGreetings: number;
}

namespace myLib1 {
	const foo: number;
	namespace myLib2 {
	  let bar: string;
	  namespace myLib3 {
		let baz: object;
	  }
	}
}

针对该规则的 正确 TypeScript 代码示例:

🌐 Examples of correct TypeScript code for this rule:

在线运行
/* eslint init-declarations: ["error", "never"] */

declare const foo: number;

declare namespace myLib {
	let numberOfGreetings: number;
}

interface GreetingSettings {
	greeting: string;
	duration?: number;
	color?: string;
}
在线运行
/* eslint init-declarations: ["error", "always"] */

declare const foo: number;

declare namespace myLib {
	let numberOfGreetings: number;
}

interface GreetingSettings {
	greeting: string;
	duration?: number;
	color?: string;
}

何时不使用

🌐 When Not To Use It

当你对变量的初始化方式漠不关心时。

🌐 When you are indifferent as to how your variables are initialized.

版本

此规则是在 ESLint v1.0.0-rc-1 中引入。

资源