typescript系列教程_08-interface的使用1.mp4 (139.34MB)

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

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

可选参数使用 ?


interface labelobj{
    label:string
    width?: number
}

function printLabel(lableObject: labelobj){
    console.log(lableObject)
}

printLabel({label:"label"})
printLabel({label:'abc',width:10})

readonly 不能修改的值

interface Point{
    readonly x:number
    readonly y:number
}

let p1:Point = {x:10,y:5}
p1.x = 20  // Cannot assign to 'x' because it is a read-only property.