function asyncOperation<T>(callback: (result: T, error?: Error) => void) {
// 模拟异步操作,这里简单随机决定成功或失败
const isSuccess = Math.random() > 0.5;
if (isSuccess) {
const data: T = {} as T;
callback(data);
} else {
const error = new Error('操作失败');
callback(null as unknown as T, error);
}
}
// 调用处理number数组
asyncOperation<number[]>((result, error) => {
if (error) {
console.error('处理number数组失败:', error.message);
} else {
console.log('处理number数组成功:', result);
}
});
// 调用处理{name: string}对象
asyncOperation<{name: string}>((result, error) => {
if (error) {
console.error('处理{name: string}对象失败:', error.message);
} else {
console.log('处理{name: string}对象成功:', result);
}
});