You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// The parameters 'x' and 'y' have the type numberletmyAdd=function(x: number,y: number): number{returnx+y;};// myAdd has the full function typeletmyAdd2: (baseValue: number,increment: number)=>number=function(x,y){returnx+y;};
functionbuildName(firstName: string,lastName: string){returnfirstName+" "+lastName;}letresult1=buildName("Bob");// error, too few parametersExpected2arguments,butgot1.letresult2=buildName("Bob","Adams","Sr.");// error, too many parametersExpected2arguments,butgot3.letresult3=buildName("Bob","Adams");// ah, just right
functionbuildName(firstName: string,lastName?: string){if(lastName)returnfirstName+" "+lastName;elsereturnfirstName;}letresult1=buildName("Bob");// works correctly nowletresult2=buildName("Bob","Adams","Sr.");// error, too many parametersExpected1-2arguments,butgot3.letresult3=buildName("Bob","Adams");// ah, just right
functionbuildName(firstName: string,lastName="Smith"){returnfirstName+" "+lastName;}letresult1=buildName("Bob");// works correctly now, returns "Bob Smith"letresult2=buildName("Bob",undefined);// still works, also returns "Bob Smith"letresult3=buildName("Bob","Adams","Sr.");// error, too many parametersExpected1-2arguments,butgot3.letresult4=buildName("Bob","Adams");// ah, just right
functionbuildName(firstName="Will",lastName: string){returnfirstName+" "+lastName;}letresult1=buildName("Bob");// error, too few parametersExpected2arguments,butgot1.letresult2=buildName("Bob","Adams","Sr.");// error, too many parametersExpected2arguments,butgot3.letresult3=buildName("Bob","Adams");// okay and returns "Bob Adams"letresult4=buildName(undefined,"Adams");// okay and returns "Will Adams"
剩余参数
和 ES6 中的剩余参数类似,用 ... 表示剩余参数,它可以看出把所有的可选参数都放进一个数组:
functionbuildName(firstName: string, ...restOfName: string[]){returnfirstName+" "+restOfName.join(" ");}// employeeName will be "Joseph Samuel Lucas MacKinzie"letemployeeName=buildName("Joseph","Samuel","Lucas","MacKinzie");
letsuits=["hearts","spades","clubs","diamonds"];functionpickCard(x: any): any{// Check to see if we're working with an object/array// if so, they gave us the deck and we'll pick the cardif(typeofx=="object"){letpickedCard=Math.floor(Math.random()*x.length);returnpickedCard;}// Otherwise just let them pick the cardelseif(typeofx=="number"){letpickedSuit=Math.floor(x/13);return{suit: suits[pickedSuit],card: x%13};}}letmyDeck=[{suit: "diamonds",card: 2},{suit: "spades",card: 10},{suit: "hearts",card: 4},];letpickedCard1=myDeck[pickCard(myDeck)];alert("card: "+pickedCard1.card+" of "+pickedCard1.suit);letpickedCard2=pickCard(15);alert("card: "+pickedCard2.card+" of "+pickedCard2.suit);
letsuits=["hearts","spades","clubs","diamonds"];functionpickCard(x: {suit: string;card: number}[]): number;functionpickCard(x: number): {suit: string;card: number};functionpickCard(x: any): any{// Check to see if we're working with an object/array// if so, they gave us the deck and we'll pick the cardif(typeofx=="object"){letpickedCard=Math.floor(Math.random()*x.length);returnpickedCard;}// Otherwise just let them pick the cardelseif(typeofx=="number"){letpickedSuit=Math.floor(x/13);return{suit: suits[pickedSuit],card: x%13};}}letmyDeck=[{suit: "diamonds",card: 2},{suit: "spades",card: 10},{suit: "hearts",card: 4},];letpickedCard1=myDeck[pickCard(myDeck)];alert("card: "+pickedCard1.card+" of "+pickedCard1.suit);letpickedCard2=pickCard(15);alert("card: "+pickedCard2.card+" of "+pickedCard2.suit);
函数中的类型注解
函数可以指定每个参数和返回值的类型:
当省略返回值的类型注解,
Typescript
会自动根据返回的值进行类型推断。函数类型
像其他变量的注解一样,也可以对一个变量标注为函数类型,格式为
(a: number, b:number) => number
, 如下:注意
x
和y
只是提供占位功能,可以任意取名类型推断
可以只写等式一边的类型注解,其余一边
Typescript
会自动根据语言环境自动进行类型推断:可选和默认参数
在
Typescript
中函数的参数默认是都是必须的,所以在函数调用时传递参数的数量必须和声明时要相同:在
JavaScript
中,函数的每个参数都是可选的,用户可以根据情况传递,没有传参的值默认为undefined
。而在TypeScript
中,要想让一个参数可选,可以把类型注解改为?:
:注意的是,可选参数需要跟在必录参数后面。另外,我们可以为一个参数提供默认值,在用户没传或者传
undefined
使用它:跟随在必录参数后面的可选参数和默认参数可以看成是等价的:
等于:
和可选参数不一样的是,默认参数可以在必录参数前面出现。这个时候需要使用默认值,必须显示传递
undefiend
值:剩余参数
和
ES6
中的剩余参数类似,用...
表示剩余参数,它可以看出把所有的可选参数都放进一个数组:同时支持函数类型注解和声明:
函数重载
有一些函数的参数类型可以是多种类型的,并且返回类型也可以是不确定的:
上面函数如果传入一个对象,则返回一个
number
。如果传入一个数字,则返回卡片对象。那在TypeScript
中如何准确的描述这些不同参数的类型呢?那就是函数重载,它允许我们定义一系列不同情况的参数类型注解,并且一般遵循从具体到最模糊的顺序进行声明:在函数调用时,
TypeScript
会从重载函数列表第一个开始进行类型匹配,如果参数匹配成功,就会根据这个声明进行类型检查。The text was updated successfully, but these errors were encountered: