作为函数的参数类型的定义

  1. interface labelobj{
  2. label:string
  3. }
  4. function printLabel(lableObject: labelobj){
  5. console.log(lableObject)
  6. }
  7. printLabel({label:"label"})

参数后面加上?表示可选参数

  1. interface labelobj{
  2. label:string
  3. width?: number
  4. }
  5. function printLabel(lableObject: labelobj){
  6. console.log(lableObject)
  7. }
  8. printLabel({label:"label"})
  9. printLabel({label:'abc',width:10})

readonly 不能修改的值

  1. interface Point{
  2. readonly x:number
  3. readonly y:number
  4. }
  5. let p1:Point = {x:10,y:5}
  6. p1.x = 20 // Cannot assign to 'x' because it is a read-only property.