TypeScript学习笔记

177 阅读2分钟

基本类型

let isDone: boolean = false
let age: number = 1
let binaryNumber: number = 0b1111
let firstName: string = 'winty'
let message: string = `test:${isDone}`
✨:undefined null 是所有类型的子类型
let num :number = null
let num2 :number = undefined
✨:any:任何操作都会变成any类型 没有类型检查
let notSure: any = 4
notSure = 'guess'
notSure = truen
otSure.name = '1sas'

声明变量为多种类型:

let numberOrString: number | string = 234
numberOrString = 'sda'
//numberOrString=true 报错

✨:不允许数组里面出现其他的类型定义

let arrOfNumbers: number[] = [1,2,3,4]
arrOfNumbers.push(5)
// arrOfNumbers.push('sas') 报错
类数组
function test() {  // 类数组  
    console.log(arguments);  
    arguments.length  
    // let arr: any[] = arguments 报错
}

✨:tuple 类型与数据一一对应

let user:[string,number] =['winty',23]

✨:枚举类型

// const 常量枚举
const enum Direction {  
    Up='UP',Down='Down',Left='Left',Right='Right'
}console.log(Direction.Up);// console.log(Direction[0]);
const value = 'UP'
if(value === Direction.Up){  
    console.log('it is up'); 
 }

FUNCTION:

function add(x:number,y:number,z?:number):number {
//z为可选参数,可选参数只能放在最后  
//赋默认值后,自动转为可选参数  
//:number 为返回值类型  
    if(typeof z === 'number'){    
        return x+y+z  
    }else{    
        return x+y  
    }
}
let result = add(2,3,1)
函数表达式:
const add2 = function(x:number,y:number,z?:number,a:number = 10):number {
//z为可选参数,可选参数只能放在最后  
//赋默认值后,自动转为可选参数  
//:number 为返回值类型 
 if(typeof z === 'number'){    
    return x+y+z  
  }else{    
    return x+y  
  }
}
函数类型
const add3:(x:number,y:number,a:number,z?:number) => number = add2
//=> :声明函数类型返回值的方法

  • 类:定义了一切事物的抽象特点
  • 对象:类的实例
  • 面向对象:封装 继承 多态
public private protected [protected:子类可以访问属性]

举个🌰:

class Animal {  
//public private protected  
// protected:子类可以访问属性  
//readonly  
    static categoies:string[] = ['mammal','bird'];  
    static isAnimal(a){    
        return a instanceof Animal  
    }  
    readonly name: string;  
    constructor(name: string) {    
        this.name = name  
    }  
    run() {    
        return `${this.name} is running`  
    }
}

const snake = new Animal('lily')
protected: 属性“name”受保护,只能在类“Animal”及其子类中访问。
read-only : Cannot assign to 'name' because it is a read-only。


类上面的静态属性、方法可以直接访问
console.log(Animal.categoies);
console.log(Animal.isAnimal(snake));
private: 属性“name”为私有属性,只能在类“Animal”中访问
class Dog extends Animal {  
    bark() {    //private    
        return `${this.name} 汪汪汪`  
    }
}

readonly vs const

最简单判断该用readonly还是const的方法是看要把它做为变量使用还是做为一个属性。 做为变量使用的话用 const,若做为属性则使用readonly

interface

一个接口可以继承多个接口,创建出多个接口的合成接口。

interface Shape {
    color: string;
}

interface PenStroke {
    penWidth: number;
}

interface Square extends Shape, PenStroke {
    sideLength: number;
}

let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;

函数类型interface

除了描述带有属性的普通对象外,接口也可以描述函数类型。

为了使用接口表示函数类型,我们需要给接口定义一个调用签名。 它就像是一个只有参数列表和返回值类型的函数定义。参数列表里的每个参数都需要名字和类型。

interface SearchFunc {
  (source: string, subString: string): boolean;
}

列如:

let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
  let result = source.search(subString);
  return result > -1;
}

对于函数类型的类型检查来说,函数的参数名不需要与接口里定义的名字相匹配。 比如,我们使用下面的代码重写上面的例子:

let mySearch: SearchFunc;
mySearch = function(src: string, sub: string): boolean {
  let result = src.search(sub);
  return result > -1;
}

 如果你不想指定类型,TypeScript的类型系统会推断出参数类型,因为函数直接赋值给了 SearchFunc类型变量。 函数的返回值类型是通过其返回值推断出来的(此例是 falsetrue)。 

let mySearch: SearchFunc;
mySearch = function(src, sub) {
    let result = src.search(sub);
    return result > -1;
}