no-mutable-public-property-of-construct β
β
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 disallows making public properties of a CDK Construct mutable (i.e. disallow defining public properties without the readonly modifier).
Constructs often represent stateful AWS resources. Making these public properties readonly helps prevent unintended modifications after the Construct has been instantiated, leading to more predictable and maintainable code.
Therefore, it is recommended to specify the readonly modifier for public properties.
(This rule applies only to classes that extends from Construct or Stack.)
π§ How to use β
js
// eslint.config.mjs
export default defineConfig([
{
// ... some configs
rules: {
"cdk/no-mutable-public-property-of-construct": "error",
},
},
]);β Correct Example β
ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";
export class MyConstruct extends Construct {
// β
public readonly properties are allowed
public readonly bucket: IBucket;
}β Incorrect Example β
ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";
export class MyConstruct extends Construct {
// β public properties should be readonly
public bucket: IBucket;
}