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 parameter's type annotation is an object typefunctionprintCoord(pt: {x: number;y: number}){console.log("The coordinate's x value is "+pt.x);console.log("The coordinate's y value is "+pt.y);}printCoord({x: 3,y: 7});
两个属性的类型注释了参数 - x 和 y - 均为 number 型。你可以使用 , 或 ; 来分隔属性,不指定类型就会假定为any类型
可选属性
functionprintName(obj: {first: string;last?: string}){// ...}// Both OKprintName({first: "Bob"});printName({first: "Alice",last: "Alisson"});
对象类型
两个属性的类型注释了参数 - x 和 y - 均为 number 型。你可以使用 , 或 ; 来分隔属性,不指定类型就会假定为any类型
可选属性
在函数入参名字后加入? ,相当于定义该属性不是非必须的,类似java的有参构造器。
注意点,如果该属性不存在需要进行undefined判断
The text was updated successfully, but these errors were encountered: