@@ -3,7 +3,7 @@ import { IConstruct } from 'constructs';
33import { Group } from './group' ;
44import {
55 AccountPrincipal , AccountRootPrincipal , AnyPrincipal , ArnPrincipal , CanonicalUserPrincipal ,
6- FederatedPrincipal , IPrincipal , PrincipalBase , PrincipalPolicyFragment , ServicePrincipal , ServicePrincipalOpts ,
6+ FederatedPrincipal , IPrincipal , PrincipalBase , PrincipalPolicyFragment , ServicePrincipal , ServicePrincipalOpts , validateConditionObject ,
77} from './principals' ;
88import { normalizeStatement } from './private/postprocess-policy-document' ;
99import { LITERAL_STRING_KEY , mergePrincipal , sum } from './util' ;
@@ -380,6 +380,8 @@ export class PolicyStatement {
380380 */
381381 public addCondition ( key : string , value : Condition ) {
382382 this . assertNotFrozen ( 'addCondition' ) ;
383+ validateConditionObject ( value ) ;
384+
383385 const existingValue = this . _condition [ key ] ;
384386 this . _condition [ key ] = existingValue ? { ...existingValue , ...value } : value ;
385387 }
@@ -670,19 +672,15 @@ export enum Effect {
670672 * Condition for when an IAM policy is in effect. Maps from the keys in a request's context to
671673 * a string value or array of string values. See the Conditions interface for more details.
672674 */
673- export type Condition = any ;
675+ export type Condition = unknown ;
674676
675- // NOTE! We'd ideally like to type this as `Record<string, any>`, because the
676- // API expects a map which can take either strings or lists of strings.
677- //
678- // However, if we were to change this right now, the Java bindings for CDK would
679- // emit a type of `Map<String, Object>`, but the most common types people would
680- // instantiate would be an `ImmutableMap<String, String>` which would not be
681- // assignable to `Map<String, Object>`. The types don't have a built-in notion
682- // of co-contravariance, you have to indicate that on the type. So jsii would first
683- // need to emit the type as `Map<String, ? extends Object>`.
677+ // NOTE! We would have liked to have typed this as `Record<string, unknown>`, but in some places
678+ // of the code we are assuming we can pass a `CfnJson` object into where a `Condition` is expected,
679+ // and that wouldn't typecheck anymore.
684680//
685- // Feature request in https://github.com/aws/jsii/issues/1517
681+ // Needs to be `unknown` instead of `any` so that the type of `Conditions` is
682+ // `Record<string, unknown>`; if it had been `Record<string, any>`, TypeScript would have allowed
683+ // passing an array into `conditions` arguments (where it needs to be a map).
686684
687685/**
688686 * Conditions for when an IAM Policy is in effect, specified in the following structure:
@@ -877,4 +875,4 @@ class OrderedSet<A> {
877875 public direct ( ) : readonly A [ ] {
878876 return this . array ;
879877 }
880- }
878+ }
0 commit comments