- 直接命名
- 使用type或interface声明类型(类型别名)
// 定义变量类型
type Point = {
x: number,
y: number
}
function addNumber(pt: Point) {
return pt.x + pt.y
}
addNumber({ x: 123, y: 321 })
// 接口-为对象定义
// interface Animal {
// name: string
// }
// // 接口扩展
// interface Bear extends Animal {
// age: number
// }
// const b1: Bear = {
// name: 'lx',
// age: 131
// }
type Animal = {
name: string
}
type Bear = Animal & {
age: number
}
// 向现有类型添加新字段
interface MyWindow {
count: number
}
interface MyWindow {
title: string
}
- 类型断言
const myCanvas = document.getElementById('') as HTMLCanvasElement
const myCanvas2 = <HTMLCanvasElement>document.getElementById('')
下一篇:三.使用webpack包装