函数声明类型定义格式:
function fn(a: number, b: number): number {
return a+b
}
函数表达式类型定义格式:
const fn = (a: number, b: number): number => a+b
可以将拥有相同形参和实参的函数当做一个整体来定义:
const fn: (n1: number, n2: number) =>number = (a,b) => a+b
其中类型定义部分可提炼:
type NumFn = (n1: number, n2: number) => number
const fn: NumFn = (a, b) => a+b
const minus: NumFn = (c, d) => c-d
fn(1, '2') // 报错
fn(3, 4) // 7
minus(4, 5) // -1