Skip to content

Commit 0fc5514

Browse files
committed
return false for Construct.isConstruct
1 parent a82f214 commit 0fc5514

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/aws-cdk-lib/core/lib/private/detached-construct.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Construct, IConstruct } from 'constructs';
22
import type { ResourceEnvironment } from '../environment';
33
import { UnscopedValidationError } from '../errors';
44

5+
const CONSTRUCT_SYM = Symbol.for('constructs.Construct');
6+
57
/**
68
* Base class for detached constructs that throw UnscopedValidationError
79
* when accessing node, env, or with() methods.
@@ -28,6 +30,14 @@ export abstract class DetachedConstruct extends Construct implements IConstruct
2830
Object.defineProperty(this, 'node', {
2931
get() { throw new UnscopedValidationError(errorMessage); },
3032
});
33+
34+
// Despite extending Construct, DetachedConstruct doesn't work like one.
35+
// So we try to not pretend that this is a construct as much as possible.
36+
Object.defineProperty(this, CONSTRUCT_SYM, {
37+
value: false,
38+
enumerable: false,
39+
writable: false,
40+
});
3141
}
3242

3343
public get env(): ResourceEnvironment {

packages/aws-cdk-lib/core/test/private/detached-construct.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Construct } from 'constructs';
12
import { UnscopedValidationError } from '../../lib/errors';
23
import { DetachedConstruct } from '../../lib/private/detached-construct';
34

@@ -21,4 +22,10 @@ describe('DetachedConstruct', () => {
2122
expect(() => construct.env).toThrow(UnscopedValidationError);
2223
expect(() => construct.env).toThrow('test error message');
2324
});
25+
26+
test('returns false for Construct.isConstruct', () => {
27+
const construct = new TestDetachedConstruct('test error message');
28+
29+
expect(Construct.isConstruct(construct)).toBe(false);
30+
});
2431
});

0 commit comments

Comments
 (0)