var arr1:number[] = [1,2,3]
var arr2:Array<string> = ['hello','world']
enum Color {Red, Blue, Green};
var x:Color = Color.Green;
var x1:number = Color.Green;
enum Color1 {Red, Blue = 12, Green };
var a:string = Color1[0]
var a1:string = Color1[13]
enum Color2 {Red = 11, Blue = 2, Green = 32};
var b:string = Color2[32]
var is:any = 'asda'
var arr3:any[] = [1,'hello', false]
function tell():string {
return 'aaa'
}
function tell0():void{
}
function add(a:number, b:number):string{
return a+b+'aaa'
}
var myAdd:(name:string,age:number) => number = function(n:string,a:number):number{
return a;
}
function peopleName(firstName:string, ...restOfName:string[]){
return firstName+""+restOfName.join("-");
}
console.log(peopleName("hang"))
console.log(peopleName("iwn","ime","aa","bb"))
function peopleName2(...restOfName:string[]){
return restOfName.join("-");
}
console.log(peopleName2("x","y"))
var people = {
name: ["iwn","ime","aa","bb"],
getName:function(){
return () => {
var i = Math.floor(Math.random()*4);
return {
n: this.name[i]
}
}
}
}
var xx = people.getName();
console.log(xx().n)
function attr(name:string):string;
function attr(name:number):number;
function attr(nameor:any):any{
if(nameor && typeof nameor === "string"){
console.log(nameor+" is string")
}else{
console.log(nameor+" is number")
}
}
attr("hello")
attr(222)
class Person{
name:string;
age: number;
private sex: string;
constructor(name:string, age:number, sex:string){
this.name = name;
this.age = age;
this.sex = sex;
}
print(){
return this.name+":"+this.age+":"+this.sex;
}
}
var p = new Person("Fane",23,"man")
console.log(p.print())
class Student extends Person{
school: string;
constructor(school:string){
super("Lilei", 24,"man");
this.school = school;
}
print(){
return this.name+":"+this.age+":"+this.school
}
}
var s = new Student("Jialidun School")
console.error(s,s.print())
class Hello{
private _age: number
tell(){
return this._age
}
get age():number{
return this._age
}
set age(newage: number){
if(newage > 200 || newage < 0){
alert("Error Age")
}else{
this._age = newage
}
}
}
var h = new Hello();
h.age = 100
console.log(h.age, h.tell())
class Person2{
name:string;
tell(){
console.log(this.name)
}
}
var p2 = new Person2();
p2.name = "zhaoqiang"
p2.tell()
class Person3{
static myname:string;
tell(){
console.log(Person3.myname)
}
}
var p3 = new Person3();
Person3.myname = "zhaoqiang--3"
p3.tell()
var myP2: Person2;
myP2 = new Person2();
function printLabel(obj:{label:string}){
console.log(obj.label)
}
var myObj = {label: "Hello!"}
printLabel(myObj)
interface LabelValue{
label: string;
age?: number;
}
function printLabel2(labelObj: LabelValue){
console.log(labelObj)
}
var myObj2 = {label: "Hello-2!"}
printLabel(myObj2)
interface SearchFunc{
(source: string,subString:string): boolean
}
var mySearch:SearchFunc;
mySearch = function(src:string,sub:string){
var res = src.search(sub);
if(res != 1){
return true;
}else{
return false;
}
}
interface StringArray{
[index:number]:string;
}
interface ClockInterface{
currentTime: Date;
setTime(d:Date);
}
class Clock implements ClockInterface{
currentTime: Date;
setTime(d:Date){
this.currentTime = d;
}
constructor(h:number, m:number){
}
}
interface Shape{
color: string
}
interface Square extends Shape{
sideLength: number;
}
var ss = <Square>{};
ss.color = "blue";
ss.sideLength = 10;
interface Shape{
color: string
}
interface Stoke{
width: number;
}
interface Square extends Shape,Stoke{
sideLength: number;
}
interface Counter{
interval: number;
reset():void;
(start:number):string;
}
var c:Counter;
c(10);
c.reset();
複製代碼
// 泛型<T>:不肯定參數類型時候,定義泛型【此處不必定必須爲T,這只是個習慣】
function THello<T>(arg: T):T{
return arg;
}
// 在使用的時候再肯定類型
var toutput = THello<string>("Hello world");
console.warn(toutput)
// 泛型應用再舉例
function THello1<T>(str: T[]):T[]{
console.log(str.length)
return str;
}
var list:Array<string> = THello1<string>(["1","2","3"]);
// 泛型與lambads表達式
// 指定一個泛型函數。其 參數爲K類型,返回也爲K類型的THello泛型
var myHello:<K>(arg:K) => K = THello;
console.log(myHello("hello!!!"))
// 與上面同樣,換種寫法
var myHello2:{<T>(arg: T):T} = THello;
// 泛型類
class HelloNumber<T>{
Ten:T;
add:(x:T,y:T) =>T;
}
var myHelloNumber = new HelloNumber<number>();
myHelloNumber.Ten = 10;
myHelloNumber.add = function(x,y){
return x+y;
}
console.log(myHelloNumber.Ten) // 10
console.log(myHelloNumber.add(10,12)) // 22
複製代碼