no-mutable-public-fields
✅ Using recommended in an ESLint configuration enables this rule.
🔧 Some problems reported by this rule are automatically fixable by the --fix ESLint command line option
This rule disallow making public variables of a class mutable.
(This rule applies only to classes that extends from Construct
or Stack
.)
It's not good to have mutable public variables, because it can lead to unintended side effects.
✅ Correct Example
ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";
export class MyConstruct extends Construct {
// ✅ Can use readonly
public readonly bucket: IBucket;
}
❌ Incorrect Example
ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";
export class MyConstruct extends Construct {
// ❌ Shouldn't use mutable
public bucket: IBucket;
}