Within my Typescript array, I have two classes:
StripeCardModel | StripeBankModel
Both of these classes extend
StripePaymentModel
My goal is to locate a specific class within the array that can potentially contain instances of both classes. Once found, I intend to utilize the achAccountInterfaceObject
function on that class, as it will be guaranteed to be an instance of StripeBankModel
.
let bankPaymentMethod = this.user.stripePaymentMethods.find( paymentMethod => paymentMethod instanceof StripeBankModel );
if ( bankPaymentMethod ) {
this.bankAccount = bankPaymentMethod.achAccountInterfaceObject;
}
However, I am encountering a compile time error in Typescript:
Property 'achAccountInterfaceObject' does not exist on type 'StripeCardModel | StripeBankModel'.
Property 'achAccountInterfaceObject' does not exist on type 'StripeCardModel'.
Is there a way to work with multityped arrays in Typescript without experiencing such errors?
I've faced similar issues with switch cases like
function abc() : Foo|Boo {
switch ( a.constructor )
{
case "Foo":
a.boo();
case "Bar":
a.doo();
}
}
While striving for clean code, Typescript seems to impose limitations that hinder my progress. Breaking up the code into separate functions based on class doesn't seem ideal when dealing with classes that share a common inheritance or functionality.