no-construct-in-public-property-of-construct β
β
Using recommended in an ESLint configuration enables this rule.
This rule enforces specifying interfaces for read-only resources (e.g. IBucket) for public properties of a CDK Construct.
When AWS resource Constructs (e.g. Bucket) implement interfaces for read-only resources (e.g. IBucket), it is recommended to specify the read-only resource interface (e.g. IBucket) for Construct public properties. This helps prevent unintended resource modifications.
(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-construct-in-public-property-of-construct": "error",
},
},
]);β Correct Example β
ts
import { Construct } from "constructs";
import { IBucket } from "aws-cdk-lib/aws-s3";
import { DockerImageAsset } from "aws-cdk-lib/aws-ecr-assets";
import { MetricFilter } from "aws-cdk-lib/aws-logs";
class MyConstruct extends Construct {
// β
Read-only interfaces (e.g. `IBucket`) can be used
public readonly bucket: IBucket;
// β
Constructs that are not AWS resource constructs (e.g. `DockerImageAsset`) can be used
public readonly asset: DockerImageAsset;
// β
When there is no read-only resource interface, Construct types (e.g. `MetricFilter`) can be used
public readonly metricFilter: MetricFilter;
}β Incorrect Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
class MyConstruct extends Construct {
// β When a read-only resource interface exists, Construct types (e.g. `Bucket`) should not be used
public readonly bucket: Bucket;
}