typescript学习笔记

参考

学习:http://www.css88.com/doc/typescript/
练习:http://www.typescriptlang.org/play/index.html

类型

//1基本类型
let a: Boolean = false;
let a1: boolean = false;
console.log(a==a1)
// a="asdf" 错误
let b: number = 0;
let c: string = "asfdasd"
//数组
let d:Array<String>=["a","b"]
let e: any[] = [1, "a", "b"]
let f: boolean[] = [true, false]
//元组
let g:[string,boolean]=["aa",false]
let h:[string,boolean]
h = ["aaa", false]
//枚举
enum Color { RED = "#FF000", BLUE = "#0000FF", PINK = "#FFFF00" }
console.log(Color.RED)
//任意值:
let i: any = "haha"
i = 2;
i = false;
//空void
function show():void { 
    console.log("show")
}
// null和undefined
let j: null;
// j = false;错误
j = null;
let k: undefined;
// k = 2;错误
k = undefined;

//never 永不存在的值的类型,或者无法到达终点返回值类型
function error(errMesg: string):never{ 
    throw new Error("aaa")
}

//注意
let z="ss"
// z = false;错误,z第一次赋值会自动推断类型,这句会报错

类和接口

和java一样:
1,接口可以多继承
2,类只能单继承,可以多实现

interface IReadFile { 
    readFile(filePath:string): boolean;
}
interface IWriteFile { 
    writeFile(data:string): boolean;
}
interface IUsb extends IReadFile,IWriteFile { 
    version: String;
    connection(): boolean;
    close(): boolean;
}
class Usb{ 
    version:string="USB";
}
class Usb1{ 
    connection():boolean { 
        return true
    }
}
class Usb2 extends Usb implements IUsb { 
    version:string="USB2.0";
    constructor() { 
        super();
    }
    connection():boolean { 
        return true
    }
    close(): boolean { 
        return false;
    }
    readFile() :boolean{ 
        return false;
    }
    writeFile(): boolean { 
        return false;
    }
}
var usb2: Usb2 = new Usb2();
console.log(usb2.version)
console.log(usb2.connection())
console.log(usb2.close())