no-unused-props β
β
Using recommended in an ESLint configuration enables this rule.
This rule enforces that all properties defined in CDK Construct props interface are actually used within the constructor.
When developing CDK Constructs, it's common to define props interface with multiple properties, but developers may forget to use some of these properties in the constructor implementation. This leads to dead code.
(This rule applies only to classes that extend Construct.)
π§ How to use β
js
// eslint.config.mjs
export default defineConfig([
{
// ... some configs
rules: {
"cdk/no-unused-props": "error",
},
},
]);β Correct Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
interface MyConstructProps {
readonly bucketName: string;
readonly enableVersioning: boolean;
}
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string, props: MyConstructProps) {
super(scope, id);
// β
All props properties are used
new Bucket(this, "MyBucket", {
bucketName: props.bucketName,
versioned: props.enableVersioning,
});
}
}β Incorrect Example β
ts
import { Construct } from "constructs";
import { Bucket } from "aws-cdk-lib/aws-s3";
interface MyConstructProps {
readonly bucketName: string;
readonly enableVersioning: boolean;
readonly unusedProp: string; // β This property is never used
}
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string, props: MyConstructProps) {
super(scope, id);
new Bucket(this, "MyBucket", {
bucketName: props.bucketName,
versioned: props.enableVersioning,
});
}
}