In my current scenario, I have the following set of definitions:
enum AccountTypes {
CHECKING = 'Checking',
SAVINGS = 'Savings',
}
export class BankAccount {
id: number;
label?: string;
type: AccountTypes;
constructor(init?: Partial<BankAccount>) {
Object.assign(this, init);
}
}
... when attempting to perform the following action:
const account = {
id: 1,
label: 'John Doe: Chase Checking ****1892',
type: 'CHECKING',
};
const something = new BankAccount(account);
An error is encountered stating:
Types of property 'type' are incompatible. Type 'string' is not assignable to type '"CHECKING" | "SAVINGS" | undefined'.
Further experiments were conducted using:
export class BankAccount {
id: number;
label?: string;
type: keyof typeof AccountTypes;
constructor(init?: Partial<BankAccount>) {
Object.assign(this, init);
}
}
However, this approach did not yield desired results.
Note:
This particular case presents a unique challenge. It involves defining a `js` file with specific logic for use in mock tests. Essentially, it necessitates translating plain object data (resembling server responses) into test scenarios, leading to the aforementioned compatibility issue.