面试题答案
一键面试function collectParams(str: string, num?: number, ...bools: boolean[]): {
stringParam: string;
numberParam?: number;
booleanParams: boolean[];
} {
if (typeof str!=='string') {
throw new Error('第一个参数必须是字符串');
}
if (num!== undefined && typeof num!== 'number') {
throw new Error('第二个参数如果提供,必须是数字');
}
bools.forEach((bool, index) => {
if (typeof bool!== 'boolean') {
throw new Error(`布尔值参数列表中第 ${index + 1} 个参数必须是布尔值`);
}
});
return {
stringParam: str,
numberParam: num,
booleanParams: bools
};
}