In my project, I have defined two interfaces called User
and BankUser
. The structure of the interface for BankUser
looks like this:
interface BankUser extends User {
banks: { [bank_id: string]: string};
isSuper: boolean;
}
I am working on a function where I need to pass either a User
or a BankUser
, and I want the output to always be a BankUser
. If the input is a User, it should automatically include the default properties for a BankUser
.
const cleanedUser = (user: User | BankUser): BankUser => {
const {uid, displayName, email, phoneNumber, photoURL, banks = {}, isSuper = false} = user;
return {uid, displayName, email, phoneNumber, photoURL, banks, isSuper} as BankUser;
}
When trying to implement this function, I encountered two TS2339 errors stating that 'banks' and 'isSuper' do not exist on type User | BankUser
. An example of the error message is:
TS2339: Property 'banks' does not exist on type
User | BankUser
While adding // @ts-ignore
above the function resolves the issue temporarily, I prefer to find a more elegant solution. Can someone suggest a better way to handle this situation?