I'm facing a challenge with a function parameter that can accept either a string
or an array of strings. The issue arises when trying to pass this parameter to a toaster service, which only accepts the string
type. As a result, when using join(' ')
, I encounter an error message:
Property 'join' does not exist on type 'string | string[]'. Property 'join' does not exist on type 'string'.
This prevents the application from compiling successfully.
showToaster(msg: string, customClass: string | string[]) {
let cstmClass: any;
switch (typeof customClass) {
case 'string':
cstmClass = customClass;
break;
case 'object':
cstmClass = customClass.join(' ');
break;
}
this.toastrService.show(msg, null, {
toastClass: cstmClass,
timeOut: 3500
});
}