Skip to content

Latest commit

Β 

History

History
70 lines (53 loc) Β· 1.99 KB

no-undeclared-class-members.md

File metadata and controls

70 lines (53 loc) Β· 1.99 KB

no-undeclared-class-members

πŸ“ Require class members to be declared.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ’‘ This rule is manually fixable by editor suggestions.

Class members should be declared when they are used through this. Declaring fields, methods, getters, and setters in the class body makes typos visible and gives readers the class shape upfront.

Constructor assignments like this.name = name are treated as declarations. Classes with extends are ignored because the accessed member may come from a superclass. Static this, private members, computed member access, and dynamic initialization patterns like Object.assign(this, data) are unsupported.

Editor suggestions are offered for simple non-constructor assignments like this.name = name when a declaration can be inserted cleanly. Reads, calls, compound assignments, updates, and constructor assignments are reported without a suggestion.

Examples

// ❌
class Foo {
	getName() {
		return this.name;
	}
}

// βœ…
class Foo {
	name;

	getName() {
		return this.name;
	}
}
// ❌
class Foo {
	callName() {
		this.name();
	}
}

// βœ…
class Foo {
	name() {}

	callName() {
		this.name();
	}
}
// βœ…
class Foo {
	constructor(name) {
		this.name = name;
	}

	getName() {
		return this.name;
	}
}

Note

Declaring a class field like name; creates an own property with the value undefined on every instance before any method runs. This can change observable behavior, for example Object.hasOwn(instance, 'name'), so this rule provides editor suggestions instead of an autofix.