diff --git a/packages/aws-cdk-lib/core/lib/cfn-resource.ts b/packages/aws-cdk-lib/core/lib/cfn-resource.ts index c6c069f27e71f..a4dfd3066aaf4 100644 --- a/packages/aws-cdk-lib/core/lib/cfn-resource.ts +++ b/packages/aws-cdk-lib/core/lib/cfn-resource.ts @@ -4,7 +4,7 @@ import { CfnCondition } from './cfn-condition'; /* eslint-disable import/order */ import { CfnRefElement } from './cfn-element'; import { CfnCreationPolicy, CfnDeletionPolicy, CfnUpdatePolicy } from './cfn-resource-policy'; -import { Construct, IConstruct, Node } from 'constructs'; +import { Construct, Node } from 'constructs'; import { addDependency, obtainDependencies, removeDependency } from './deps'; import { CfnReference } from './private/cfn-reference'; import { Reference } from './reference'; @@ -34,10 +34,10 @@ export interface CfnResourceProps { */ export class CfnResource extends CfnRefElement { /** - * Check whether the given construct is a CfnResource + * Check whether the given object is a CfnResource */ - public static isCfnResource(construct: IConstruct): construct is CfnResource { - return (construct as any).cfnResourceType !== undefined; + public static isCfnResource(x: any): x is CfnResource { + return x !== null && typeof(x) === 'object' && x.cfnResourceType !== undefined; } // MAINTAINERS NOTE: this class serves as the base class for the generated L1 diff --git a/packages/aws-cdk-lib/core/test/cfn-resource.test.ts b/packages/aws-cdk-lib/core/test/cfn-resource.test.ts index d8c8fab44d5b5..78230863d3f51 100644 --- a/packages/aws-cdk-lib/core/test/cfn-resource.test.ts +++ b/packages/aws-cdk-lib/core/test/cfn-resource.test.ts @@ -412,4 +412,28 @@ describe('cfn resource', () => { delete process.env.CDK_DEBUG; } }); + + test('isCfnResource returns true with a CfnResource', () => { + const app = new core.App(); + const stack = new core.Stack(app, 'Stack'); + const res = new core.CfnResource(stack, 'SomeCfnResource', { + type: 'Some::Resource', + }); + + // THEN + expect(core.CfnResource.isCfnResource(res)).toBe(true); + }); + + test('isCfnResource returns false with a construct', () => { + const app = new core.App(); + const stack = new core.Stack(app, 'Stack'); + + // THEN + expect(core.CfnResource.isCfnResource(stack)).toBe(false); + }); + + test('isCfnResource returns false with undefined', () => { + // THEN + expect(core.CfnResource.isCfnResource(undefined)).toBe(false); + }); });