NOTE: I realize this question is extremely similar to this thread but it does not apply to Classes and, since classes cannot extend an interface, I am a bit stuck here.
I ran into an interesting question I would like to solve for my own learning. I am trying to type my class so that one of two properties is required for a transaction. In this specific case, I need either bankAccountNumber
or encryptedBankAccountNumber
present.
export class AchDetails {
'bankAccountNumber'?: string;
'encryptedBankAccountNumber'?: string;
'type'?: AchDetails.TypeEnum;
static discriminator: string | undefined = undefined;
static attributeTypeMap: Array<{name: string, baseName: string, type: string}> = [
{
"name": "bankAccountNumber",
"baseName": "bankAccountNumber",
"type": "string"
},
{
"name": "encryptedBankAccountNumber",
"baseName": "encryptedBankAccountNumber",
"type": "string"
}
];
static getAttributeTypeMap() {
return AchDetails.attributeTypeMap;
}
}
export namespace AchDetails {
export enum TypeEnum {
Ach = 'ach',
AchPlaid = 'ach_plaid'
}
}
I wanted to use the solution from the issue linked above, but this does not help as (from what I understand) the Class can't use external typings:
interface AchBaseDetails {
'bankAccountNumber'?: string;
'encryptedBankAccountNumber'?:string;
}
type RequireBankNumberType<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>>
& {
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>
}[Keys]
export type RequiredAchDetails = RequireBankNumberType<AchBaseDetails, 'bankAccountNumber' | 'encryptedBankAccountNumber'>
Is there any way to do this so that it will work with the Class in this instance?