如何接受arguments参数
function add(...args: string[]) {
let list4: IArguments = arguments;
}
add('1', '2')
自定义一个args
interface A1 {
callee: Function,
length: number,
[index: number]: any
}
function adds(...args: string[]) {
let list4: A1 = arguments;
}
adds('1', '2')
多个参数
function add1(a: number, b: number): number {
return a + b;
}
const add2 = (a: number, b: number): number => {
return a + b;
}
默认值
const add3 = (a: number = 10, b: number = 15): number => {
return a + b;
}
可选参数
const add4 = (a?: number, b?: number): void => {
}
定义一个对象参数
interface User {
name: string,
age: number
}
function getName(data: User): User {
return data;
}
定义this参数
//ts 可以定义this 必须是第一个参数
interface Obj {
list: number[],
add: (this: Obj, num: number) => void
}
let obj: Obj = {
list: [1, 2, 3, 4],
add(this: Obj, num: number) {
this.list.push(num);
console.log(this.list)
}
}