I combined several interfaces to create a new type. But for some reason, TypeScript is treating my type as never
. Can you help me figure out why?
type OrganizationAccount = Account & DetailedAccount & AccountSelection;
interface Account {
name: string;
created_on: string;
}
interface DetailedAccount {
account_type: string;
status: string;
}
interface AccountSelection {
isSelected?: boolean;
}
Initially, my API request only returns the basic Account
properties like name
and created_on
.
When the user clicks a button to fetch more details, another request is made which adds the extra DetailedAccount
properties to the account object.
function getAccountDetails(account: OrganizationAccount) {
setOrganizationAccounts((prevState: OrganizationAccount[]) => {
return prevState.map(organizationAccount => {
return organizationAccount.name === account.name
? {
...organizationAccount,
...accountDetailsRes,
}
: organizationAccount;
});
});
});
}
Later in the code, I check for the presence of account_type
to display different results. However, TypeScript seems to mistakenly infer that the account
variable is of type never
.
function accountType(account: OrganizationAccount) {
if (!('account_type' in account)) { // Here account === OrganizationAccount
console.log('account', account); // Here account === never
return 'Loading...';
}
return account.account_type;
}