TypeScript数据类型

基础数据类型

number、string、boolean、 null 和 undefined、object

  1. //1.基础类型使用
  2. // number:表示数值类型,例如:let age: number = 25;
  3. // string:表示字符串类型,例如:let name: string = "John";
  4. // boolean:表示布尔类型,例如:let isReady: boolean = true;
  5. // null 和 undefined:表示空值类型,例如:let data: null = null;
  6. // object:表示对象类型,例如:let person: object = { name: "John", age: 25 };\
  7. export{}
  8. let n:number=18;
  9. let str:string="张三";
  10. let b:boolean=true;
  11. let arr:number[]=[1,2,3];
  12. let arr2:Array<number>=[1,2,3];
  13. let data:undefined=undefined;
  14. let data2:null=null;
  15. let data3:object={name:"张三",age:18};

其他数据类型

元组 []

类似数组。数组只能是一种数据类型,元组多个数据类型。但元组长度不能太长,不然不好理解。

  1. //元组
  2. let tuple:[number,string]=[18,"cts"];

枚举 enum

  1. enum Color{Red,Yellow,Blue};

接口 interface

定义一组 属性(可以是数据类型或方法),不需要实现。可重复定义相同名称的接口,重名后将合并属性。

  1. //接口
  2. interface IPerson {
  3. readonly id: number;
  4. name: string,
  5. age: number,
  6. color: Color;
  7. address?:string,
  8. [propName: string]: any;//可以添加任意属性
  9. }
  10. let tom: IPerson = {
  11. id:1,
  12. name: 'Tom',
  13. age:12,
  14. gender2:'male',
  15. color:Color.Red
  16. };
  17. console.log(tom.age);

联合类型 |

类型可选择

  1. let union:string|number;
  2. union=18;
  3. union="cts";

交叉类型 &

将多个类型合并成一个新的类型

  1. interface IPrintable {
  2. print(str:string): void;
  3. }
  4. interface ILoggable {
  5. isLog?:boolean;
  6. log(): void;
  7. }
  8. let c:IPrintable & ILoggable={
  9. print(str:string){
  10. console.log("Printable")
  11. },
  12. log(){
  13. console.log("Loggable")
  14. }
  15. };
  16. c.print("你好呀");

type声明

  • 1type 用于定义类型别名,使得我们可以为一个复杂类型或者一个较长的类型声明一个简短的别名。这个别名可以在任何地方使用,以代替原始的类型。
  • 2.如果多次声明同一个变量、函数或类,这些声明会被自动合并为一个声明。这种合并称为声明合并。而 type 关键字也可以用于声明合并,允许我们扩展已有的类型声明。
  1. type MyString = string;
  2. type Point = { x: number; y: number };
  3. type Callback = (data: any) => void;
  4. let myStr:MyString="123";
  5. type Person2 = {
  6. name: string;
  7. }
  8. type User2 = {
  9. age: number;
  10. }
  11. let person: Person2 & User2;
  12. person = {
  13. name: 'Echo',
  14. age: 26,
  15. }

面向对象

类class

  1. //定义类
  2. class Person{
  3. name:string;
  4. age:number;
  5. address?:string;//?可为空
  6. [propName: string]: any;//可以添加任意属性
  7. constructor(name:string,age:number){
  8. this.name=name;
  9. this.age=age;
  10. }
  11. sayHello():void{
  12. console.log(`name:${this.name},age:${this.age}`);
  13. }
  14. }
  15. //类实例
  16. let user:Person=new Person('rose',18);
  17. user.sayHello();

继承extends

  1. class Teacher extends Person{
  2. sayHello():void{
  3. console.log( `Teacher,name:${this.name},age:${this.age}`);
  4. }
  5. }
  6. let user2:Person=new Teacher('jack',18);
  7. user2.sayHello();

其他

类型推断

不添加变量或常量数据类型。

  1. let num = 123; // 推断为 number 类型
  2. console.log(num.toFixed(2)); // 输出:123.00

类型断言 as

将数据类型转换,常用any数据转换

  1. let someValue: any = "this is a string";
  2. let strLength: number = (someValue as string).length;
  3. console.log(strLength); // 输出:16

总结

类型是TS中最重要的部分,因为TS的诞生主要就是解决JS弱类型的问题,增加了代码的可读性和可维护性。

引用

博文源代码https://github.com/chi8708/TypeScriptDemo/blob/main/Basic2/01Type.ts