DISCLAIMER: While this question may seem similar to a thread on Stack Overflow, the solutions provided there do not apply to Classes. Due to the limitations in extending interfaces with classes, I'm facing a challenge.
I have encountered an intriguing problem that I would like to tackle for my own educational purposes. My goal is to define a class where either bankAccountNumber
or encryptedBankAccountNumber
must be present for a transaction.
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 implement the solution suggested in the previously linked issue, but it doesn't seem feasible as external typings cannot be used directly with Classes:
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 a way to achieve this requirement while still working within the constraints of a Class?