Skip to content

Latest commit

Β 

History

History
75 lines (60 loc) Β· 1.43 KB

File metadata and controls

75 lines (60 loc) Β· 1.43 KB

prefer-class-fields

πŸ“ Prefer class field declarations over this assignments in constructors.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§πŸ’‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Enforces declaring property defaults with class fields instead of setting them inside the constructor.

To avoid leaving empty constructors after autofixing, use the no-useless-constructor rule.

Examples

// ❌
class Foo {
	constructor() {
		this.foo = 'foo';
	}
}

// βœ…
class Foo {
	foo = 'foo';
}
// ❌
class MyError extends Error {
	constructor(message: string) {
		super(message);
		this.name = 'MyError';
	}
}

// βœ…
class MyError extends Error {
	name = 'MyError'
}
// ❌
class Foo {
	foo = 'foo';
	constructor() {
		this.foo = 'bar';
	}
}

// βœ…
class Foo {
	foo = 'bar';
}
// ❌
class Foo {
	#foo = 'foo';
	constructor() {
		this.#foo = 'bar';
	}
}

// βœ…
class Foo {
	#foo = 'bar';
}