no-public-class-fields
✅ Using recommended in an ESLint configuration enables this rule.
This rule disallows using class types for public class fields.
(This rule applies only to classes that extends from Construct
or Stack
.)
When class types are used in public fields, it creates tight coupling and exposes mutable state, so not good.
✅ Correct Examples
ts
import { Construct } from "constructs";
import { IBucket, Bucket } from "aws-cdk-lib/aws-s3";
class MyConstruct extends Construct {
// ✅ Can use interface public field
public readonly bucket: IBucket;
constructor(scope: Construct, id: string) {
super(scope, id);
this.bucket = new Bucket(this, "MyBucket");
}
}
❌ Incorrect Examples
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
class MyConstruct extends Construct {
// ❌ Shouldn't use class type public field
public readonly bucket: Bucket;
constructor(scope: Construct, id: string) {
super(scope, id);
this.bucket = new Bucket(this, "MyBucket");
}
}