1. 直接命名
    2. 使用type或interface声明类型(类型别名)
    1. // 定义变量类型
    2. type Point = {
    3. x: number,
    4. y: number
    5. }
    6. function addNumber(pt: Point) {
    7. return pt.x + pt.y
    8. }
    9. addNumber({ x: 123, y: 321 })
    10. // 接口-为对象定义
    11. // interface Animal {
    12. // name: string
    13. // }
    14. // // 接口扩展
    15. // interface Bear extends Animal {
    16. // age: number
    17. // }
    18. // const b1: Bear = {
    19. // name: 'lx',
    20. // age: 131
    21. // }
    22. type Animal = {
    23. name: string
    24. }
    25. type Bear = Animal & {
    26. age: number
    27. }
    28. // 向现有类型添加新字段
    29. interface MyWindow {
    30. count: number
    31. }
    32. interface MyWindow {
    33. title: string
    34. }
    1. 类型断言
    1. const myCanvas = document.getElementById('') as HTMLCanvasElement
    2. const myCanvas2 = <HTMLCanvasElement>document.getElementById('')