// 任意类型
function a(...args: any[]) {
let x1 = args[0];
}
// 所有参数都是字符串
function b(...args: string[]) {
let x: string = args[0];
let err: number = args[1]; // 应该用 string
}
// 前几个参数的类型可以确定,且参数个数确定,用解构类型定义
function c(...args: [string, number, any[]]) {
let x1: string = args[0];
let x2: number = args[1];
let x3: Date = args[2]; // 应该用 any[]
}
// 参数个数不定,后面的参数类型不定,解构类型
function d(...args: [string, number, ...any[]]) {
let x1: string = args[0];
let x2: number = args[1];
let x3: Date = args[2];
let e1: number = args[0]; // 应该 string
let e2: string = args[1]; // 应该 number
}
下面的示例,用 e# 和 err 表示错误的示例,x# 是正确的示例