- 语法
...args
,将所有剩余的参数
放进名为 args 的数组
中 剩余参数
必须放在函数声明时所有参数的最后
(前面有几个不限制)
放入部分参数:
function showMsg (str1: string, str2: string, ...args: string []) {
console.log(str1) // 'a'
console.log(str2) // 'b'
// 根据 args 可取出剩余参数
console.log(args) // ['c', 'd', 'e', 'f']
}
showMsg('a', 'b', 'c', 'd', 'e', 'f')
放入全部的参数:
function showMsg2 (...args: number[]) {
console.log(args) // [1, 2, 3]
}
showMsg2(1, 2, 3)