I am using vuejs.
this.$slots.eventsContent?.[0]?.data = {};
I got error from IDE.
TS2779: The left-hand side of an assignment expression may not be an optional property access.
Do you know how to get rid of error?
CodePudding user response:
Do not use optional property.
this.$slots.eventsContent[0].data = {};
If you get "property maybe undefined error" you can do like this:
if (this.$slots.eventsContent && $slots.eventsContent.length > 0) {
this.$slots.eventsContent[0].data = {};
}
Just check if the property is not undefined before doing anything. This way typescript will be sure that your variable is defined.
CodePudding user response:
You cannot assign to a property of something that is possibly undefined. If you are sure that these are not undefined, you can use this.$slots.eventsContent!.[0]!.data = {}
to ignore it, or if you only want to assign it only if the values are defined then you need nested ifs to check undefined