// 静态类型分类
// 1 基本类型 number string null undefined sybol boolean void
let num: Number = 123;
let bool: Boolean = true;
let sym: symbol;
let str: String = '123';
let any: any = 'fsdf';
any = 123;
let n: null = null;
// 2 对象类型 {}/[]/class/function
// 2.1 person 为对象类型
// 2.2 person 的对象赋值 必须【只能有 name age 】属性
let person: {
// 2.3 指定 person 对象下每一个key 的静态类型
name: String, // name 属性 为 string
age: Number // age 为number
} = {
// 2.4 给person 对象赋值
name: 'aaa',
age: 123,
// dec:'ykl dsz' // bug 多一个 少一个属性都是错
}
// 2.2 数组
// 2.21 arr 静态类型为数组;数组中元素只能为 number类型,长度随意
let arr: number[] = [1, 2, 3, 45]
// 2.22 指定数组元素;每一个索引对应什么类型数据;数组长度只能为2.
let arr1: [String, number] = ['11', 123]
// 2.23 数组中元素为任意类型,长度随意
let arr2: any[] = [1, '2', true]
// 2.3 function
// 2.31 :void 函数没有 return
function foo(a: number): void {
// return 1 // bug
}
// 2.4 class
class Point {
private x:number
// private y:string // 解决 this.y 报错
constructor(x:number, y:string) {
this.x = x;
// this.y = y; // bug Property 'y' does not exist on type 'Point'.
}
}
let p:Point = new Point(1,'1')
// 2.5
// 2.51 getSage 类型函数 没有参数 返回值为 number类型
// 问题:什么类型 赋值是什么
const getSage:()=>number = ()=>{
// return '' // bug
return 123
}
// 3 自定义类型
// 分为:类型别名/interface接口 /泛型/枚举...
// 类型别名
type blc = {
name:string,
age:number
}
let baoliangc:blc = {
name:"aaa",
age:12,
// dec:'ddd' // bug 多一个报错 少一个报错
}
ts静态类型分类
最新推荐文章于 2024-07-16 19:15:00 发布