I am a newbie to go language and I am passing some properties props?
to my CDK appstack with the following signature: props?: cdk.StackProps
Now, when I just print the variable props
on the console by typing console.log(props)
I see this (as expected):
{ env: { account: '112358132134', region: 'us-west-2' } }
However, when I do something like this: console.log(props['env'])
I get the following error:
console.log(props["env"]["account"])
I get this error:
error TS2532: Object is possibly 'undefined'.
The goal for me is to use this property for my business logic. How can I read it?
The access to account
property may fail as the env
property is optional
.
Your TypeScript settings seem to have strict null checking
turned on, thus code won't compile due to the potential TypeError
.
You can turn off the strict null checking
, although generally it's better to keep it enabled.
You can also rewrite your code to cope with possibility of env
being undefined
console.log(props.env?.account); // If You have high enough TS version
// OR
console.log(props['env'] ? props['env']['account'] : undefined);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.