TypeScript基础

发布时间 2023-10-09 12:11:36作者: BizSpring
基础类型:":"后面为变量的数据类型
布尔值:boolean
 let isDone:boolean=false
 
数字:number
TypeScript中的所有数字类型都是浮点数,类型统一都是number,支持十进制,二进制,八进制,十六进制。
let count:number=100

字符串:string
Typescript中使用string表示文本数据类型,可以使用双引号("")或者单引号('')表示字符串。
let name:string="allen" name='张三'

数组:TypeScript支持两种方式声明数组,第一种,可以在元素类型的后面跟上[],表示由此类型的元素组成一个数组,第二种是使用数组泛型,Array<元素类型>
let list:number[]; list=[1,2,3]; 
let list2:Array<number>=[1,2,3]//泛型

元组:表示一个已知元素类型和数量的数组,各个元素的类型不必相同
let x: [number, string] 
x = [1, '111']//正确 
x=['111',1]//错误

枚举类型
enum Color { Red, Green, Blue }
let c: Color = Color.Red

unknown:编程阶段未知的类型可以使用unknown来标记变量。
let notsure:unknown
notsure=4; notsure="字符串"

void 一个函数无返回值时,可以定义为void类型
function save(x:string):void{
}

any类型:任何类型,any类型的变量可以被赋予任意类型的值
定义方式:let an; 或者 let an:any
an=[];an=123;an={};an='456'
 TS在没有明确指定类型时会推测一个类型,如在定义变量时赋值,let num=123456,则推断num的类型为number,let str='张三',则str的类型为String类型。如果没有赋值则为any类型

null和undefined:TypeScript里,undefined和null两者各自有自己的类型分别是undefined和null。
let u:undefined=undefined
let n:null=null

联合类型:联合类型表示取值可以是多种类型中的一种。
let myNumber:number|string
myNumber=10;myNumber='ten'

 

定义函数
//定义一个具有2个number类型的参数,返回值为number类型的函数
function add(x: number, y: number): number {
    // 具名函数
    return x + y;
}
let myAdd=function(x:number,y:number):number{
    // 匿名函数
    return x+y;
}
//TypeScript中可以在参数名旁使用?实现课选参数的功能,即参数可为空。
function buid(firstName: string, lastNanme?: string) {
    if (lastNanme)
        return firstName + '·' + lastNanme;
    else
        return firstName;
}
箭头函数:定义匿名函数的简写语法,省略了function关键字。
let fullName = (firstName: string, lastNanme?: string) => {
    if (lastNanme)
        return firstName + '·' + lastNanme;
    else
        return firstName;
}
调用方法:fullName('allen','jon');fullName('allen')
定义函数
//定义一个具有2个number类型的参数,返回值为number类型的函数
function add(x: number, y: number): number {
    // 具名函数
    return x + y;
}
let myAdd=function(x:number,y:number):number{
    // 匿名函数
    return x+y;
}
//TypeScript中可以在参数名旁使用?实现课选参数的功能,即参数可为空。
function buid(firstName: string, lastNanme?: string) {
    if (lastNanme)
        return firstName + '·' + lastNanme;
    else
        return firstName;
}
箭头函数:定义匿名函数的简写语法,省略了function关键字。
let fullName = (firstName: string, lastNanme?: string) => {
    if (lastNanme)
        return firstName + '·' + lastNanme;
    else
        return firstName;
}
调用方法:fullName('allen','jon');fullName('allen')
类:TypeScript支持基于类的面向对象的编程方式,定义类的关键字为class,后面跟类名。
class Person {
    private name: string;
    private age: number;
    // 构造函数
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    public  GetPerson():string {
        return `name is ${this.name},age is ${this.age}`;
    }
}

let person=new Person('jack',18)
person.GetPerson;
继承:TypeScript中允许使用继承来对类进行扩展,对应的关键字为extends
class Employee extends Person {
    private department: string;
    constructor(name: string, age: number, department: string) {
        super(name, age);
        this.department = department;
    }
    public GetEmployeeInfo():string{
        return this.GetPerson()+`and work in ${this.department}`
    }
}

let employee=new Employee('allen',20,'百度')
employee.GetPerson();
employee.GetEmployeeInfo();
循环语句:for of和for in
let animals=['cate','dog','fish']
for(let item of animals)
    console.log(item)//输出 cate dog fish
for(let item in animals)
console.log(item)//输出 1 2 3,for in输出的为数组的索引