当前位置: 代码迷 >> 综合 >> typescript 接口+类+枚举+解构+扩展+接口+类+函数
  详细解决方案

typescript 接口+类+枚举+解构+扩展+接口+类+函数

热度:67   发布时间:2023-12-25 02:16:56.0
interface Person {             //     定义一个接口firstName:string    lastName:string
}
function gretter(person:Person){      return " hello "+person.firstName+""+person.lastName
}
let user:Person = {       firstName:"yee",lastName:"huang",
}
console.log(gretter(user));     //    hello yeehuang
class User {                // 定义一个类fullName:stringfirstName:stringlastName:stringconstructor(firstName:string,lastName:string){     // 构造器this.firstName=firstNamethis.lastName=lastNamethis.fullName=firstName+' '+lastName}
}let user:Person = new User("yee","huang");     // new 一个类 并且赋值
enum Color {            //枚举Red=1,Green,Blue
}let ColorName:string=Color[2]
console.log(ColorName)
let input:[number,number] = [1,2];
let [first,second]=input   
// 等价于
// let first  =  input[0];
// let second =  input[1];
function f([first,second]:[number,number]){console.log(first);console.log(second);
}
f(input);let [one,...rest]=[1,2,3,4];
console.log(one);    // 1
console.log(rest);   // 234let o={a:"foo",b:12,c:"bar"
}
let {a,...c}=o;         // 结构取值
let total=c.b+c.c.length;
console.log(total);   
let first=[1,2];
let second=[3,4];
let bothPlus=[0,...first,...second,5];    // 展开 有了这个数组合并就不用for循环再放入了
console.log(bothPlus);
console.log(first);
console.log(second);
// 数组展开let defaults={food:"spicy",price:"@10",ambiance:"sss"
}
let search={...defaults,food:"rich"}
console.log(search);// 对象展开 后面的会覆盖前面的    food:rich
function printLabel(labelObj:LabelVal){console.log(labelObj.label);//  labelObj.y=2    报错 因为是只读的
}
let myObj={size:10,label:"size 10 Object",y:1}
printLabel(myObj);interface LabelVal{     // 创建一个接口label:stringcolor?:string       // 可选属性 readonly y:number   // 只读属性,不可修改
}interface SearchFunc{    // 函数类型(source:string,subString:string):boolean
}
let mySearch:SearchFunc
mySearch=function(source:string,subString:string):boolean{let result =source.search(subString);return result>1
}interface StringArray {        // 可索引类型[index:number]:string
}let myArray:StringArraymyArray=["Bob","Fred"]let myStr:string=myArray[0];
console.log(myStr);interface Shape {color:string
}
interface PenStroke {penWidth:number
}
interface Square extends Shape,PenStroke {    // 接口继承sidelength:number
}

 

class Animal {name:stringconstructor(name:string){this.name=name}move(distance:number=0){console.log(`${this.name} moved ${distance}m`)}
}class Snake extends Animal {constructor(name:string){super(name)                      // 调用父类的方法}move(distance:number=5){console.log("slithering...")super.move(distance);}
}class Horse extends Animal {constructor(name:string){super(name);}move(distance:number=15){console.log("gogogog...")super.move(distance);}
}let sam=new Snake("Sammy");
let tom:Animal=new Horse("tommy")
sam.move();      // 调用
tom.move(12);
function buildName(firstName:string,lastName?:string,...otherName:string[]):string {    // ?可选参数 必须放在必选参数后面   ...剩余参数是个数组console.log(firstName,lastName,otherName);return firstName+""+lastName
}
buildName("1","2","23");