Here is my code. The data
is supposed to be referred to as one of the FeeReturnType
. However, it is currently referred to as a union type of AllFee[] | ApiOrFileFee[] | DataSvrFee[] | AccountFee[]
.
export type FeeType = 'ALL' | 'API' | 'FILE' | 'DATA_SVR' | 'ACCOUNT';
export interface AllFee {
name: string;
fee: number;
}
export interface ApiOrFileFee {
integrator_id: string;
integrator_name: string;
call_count: number;
fee: number;
}
export interface DataSvrFee {
integrator_id: string;
integrator_name: string;
count: number;
fee: number;
}
export interface AccountFee {
name: string;
id: string;
count: string;
user: string;
user_id: string;
fee: number;
}
export type FeeReturnType<T extends FeeType> =
T extends 'ALL' ? AllFee[] :
T extends 'API' | 'FILE' ? ApiOrFileFee[] :
T extends 'DATA_SVR' ? DataSvrFee[] :
T extends 'ACCOUNT' ? AccountFee[] :
never;
function handleFeeData<T extends FeeType>(_type: T, data: FeeReturnType<T>): void {
switch (_type) {
case 'ALL':
data.forEach(item => {
console.log('All Fee:', item.name, item.fee);
});
break;
case 'API':
case 'FILE':
data.forEach(item => {
console.log('API/File Fee:', item.integrator_id, item.call_count, item.fee);
});
break;
case 'DATA_SVR':
data.forEach(item => {
console.log('Data Svr Fee:', item.integrator_id, item.count, item.fee);
});
break;
case 'ACCOUNT':
data.forEach(item => {
console.log('Account Fee:', item.name, item.user, item.fee);
});
break;
default:
break;
}
}
const allFeeData: AllFee[] = [{ name: 'All Fee', fee: 100 }];
const apiFeeData: ApiOrFileFee[] = [{ integrator_id: '123', integrator_name: 'API Integrator', call_count: 10, fee: 50 }];
const accountFeeData: AccountFee[] = [{ name: 'Account', id: '1', count: '10', user: 'John', user_id: '100', fee: 200 }];
handleFeeData('ALL', allFeeData);
handleFeeData('API', apiFeeData);
handleFeeData('ACCOUNT', accountFeeData);