I am looking to implement an array of Payments for each Rent instance, but I also want the flexibility to pass either a single Payment object or an array of Payment objects through the constructor...
However, the issue arises when I try to assign this.payment on the second occurrence and receive an error stating: "Type '(Payment | Payment[])[]' is not assignable..."
Here's the code snippet:
class Rent {
payments: Payment[]
date: Date
constructor(date: Date, payment:Payment|Payment[]) {
this.date = date
if(payment instanceof Array){
this.payments = payment
} else {
this.payments = [payment]
}
}
PS: I understand that I could always use [Payment] even when creating just one, but I prefer having multiple options available.