type Isum = (a: string, b: string) => string;
let sum: Isum = (a: string, b: string) => a + b;
type Isum = (a: string, b: string) => void;
let sum: Isum = (a: string) => a;
- 函数可选参数
可选参数意味着可以不传,和 string | underfined 不同,string | underfined 是必须要传
function fun(a:string,b?:number){}
fun('abc')
function fun(a:string,b:number | undefined){}
fun('abc')
- 可选参数只能在参数列表中的后面
function fun(a?:string,b:number){}
let sum = (a: string = '123', b?: string): string => {
return a + b
}
sum('123')
sum('123',undefined)
sum('123','abc')
let total = (...rest: number[]): number => {
return rest.reduce((memo, current) => ((memo += current), memo))
}
let num = 123
type N = typeof num
let a:N = 123
let obj = {
name: 'zhangsan',
age: 18
}
type objType = typeof obj
type keyType = keyof typeof obj
type valueType = objType[keyType]