概念

TS是JS的一个超集,在原有的语法基础上,添加了可选静态属性和基于类的面向对象编程

特点

类型

  1. 基础类型 boolean/string/number/undefined/null/array

  2. 元组类型 tuple

  3. 枚举类型 enum

  4. 任意类型 any

  5. 未知类型 unknow

  6. 空类型 void

  7. 永不返回类型 never

  8. 对象类型

  9. 类型断言

接口

interface Vegatables { 
    name: string;
    readonly color: string; //只读属性
    size?: number; // 可选属性
    [key: string]: any; // 自定义属性
}

interface Price {
	price: number;
	unit: string
}

// 类型继承
interface Tomato extends Vegatables {
	desc: string
} 

// 交叉类型,取并集
type VegatablePrice = Vegatable**s &** Price
**//** 联合类型
****type VegatablePrice = Vegatable**s |** Price

**interface Test {
    name: string;
    add: (param: string) => string
}

class TestA implements Test {
    name: string;
    
    add(name: string) {
        return ""
    }
}**