typescript(四)ts中函数的参数和返回值的类型定义

前面我们讲到过ts的静态类型定义中的函数类型定义,先来回顾下:

const fnA: () => string = () => { return '1' }
const fnB: () => number = () => 6
const fnC: () => boolean = () => true

拓展下:在接口中如何定义函数类型呢?接口后期会讲

interface Ifn {
    (one: number, two: number): number
}
let fniA : Ifn
fniA = function (one, two) {
    return one + two
}
fniA(2, 4)
console.log(fniA(2, 4)) // 6

接下来我们分别学习下ts中如何对函数参数和函数返回值进行类型定义的

1.ts中函数参数的类型定义

函数的参数可能是一个,也可能是多个,有可能是一个变量,一个对象,一个函数,一个数组等等。

1.函数的参数为单个或多个单一变量的类型定义

function fntA(one, two, three) {
	// 参数 "two" 隐式具有 "any" 类型,但可以从用法中推断出更好的类型。
    return one + two + three
}
const aResult = fntA(1, '3', true)

修改后:

function fntA(one: number, two: string, three:

你可能感兴趣的:(前端,html,javascript,前端,vue.js)