Skip to content

Latest commit

 

History

History
58 lines (46 loc) · 1.28 KB

prefer-class-fields.md

File metadata and controls

58 lines (46 loc) · 1.28 KB

Prefer class field declarations over this assignments in constructors

💼 This rule is enabled in the ✅ recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

This rule enforces the use of class field declarations for static values, instead of assigning them in constructors using this.

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';
}