面试题答案
一键面试class InvalidNumberError extends Error {
constructor(message: string) {
super(message);
this.name = 'InvalidNumberError';
}
}
function parseNumber(str: string): number {
const num = parseFloat(str);
if (isNaN(num)) {
throw new InvalidNumberError(`无法将 "${str}" 解析为数字`);
}
return num;
}