TypeScript静态类型和对象
[2020.10.22] 第二次学习
具体学习资源来自技术胖链接
在 TypeScript 静态类型分为两种,一种是基础静态类型,一种是对象类型,是编程中非常实用的静态类型, 经常要用到.
基础静态类型
首先是基础静态类型的声明, Typescript的基础静态类型有number, string, null, undefinde, symbol, boolean,void
这些都是最常用的基础数据类型,下面声明一下这些简单的静态类型
const num : number = 111;
const myName : string = 'zyb';
const isnull : null = null;
静态对象类型
在OOP的编程中对象的作用是十分重要的,下面是对象的声明与初始化
const Female : {
name : string,
age : number
} = {//初始化
name : '李华',
age : 18
}
console.log(xiaohong.uname + xiaohong.age)
Typescript同样可以使用数组进行编程
const Females : string [] = ['小红', '翠花', '小蓝']
类类型的变量也同样可以声明, 下面是声明加实例化
class Person {}
const LiHua : Person = new Person()
Typescript还能声明函数对象, 如:
const PrintInfo : ()=>string = ()=>{return 'aaa'}
那我们现在总结一下对象类型可以有几种形式:
1.对象类型
2.数组类型
3.类类型
4.函数类型
这几种形式我们在TypeScript里叫做对象类型。