模块
export(类,接口,变量,函数) :导出声明 default(默认)
import:导入
命名空间
namespace : 定义一块私有的空间,恰好与export相反,外部看不到内部细节
装饰器
在类的上面@装饰器名称 : 不改变原有的类,动态扩展类的属性和方法
function logclass (info : any){
console.log(info)
info.prototype.apiurl = 'localhost:8080'
}
@logclass
class use{
name:string | undefined;
password : string | undefined;
}
let A = new use();
console.log(A.apiurl);//编译会报错,但是打印结果会出来
例子
// 定义一个泛型接口
interface DB<T>{
get(id:number): any;
updata(type:T , id : number):boolean;
delete(id:number):boolean;
add(info : T) : T
}
// 实现泛型接口必须是泛型类
class mysqlDB<T> implements DB<T>{
add(info: T): T {
throw new Error("Method not implemented.");
}
get(id: number) {
throw new Error("Method not implemented.");
}
updata(type: T, id: number): boolean {
throw new Error("Method not implemented.");
}
delete(id: number): boolean {
throw new Error("Method not implemented.");
}
}
class sqlyanggeDB<T> implements DB<T>{
add(info: T): T {
throw new Error("Method not implemented.");
}
get(id: number) {
throw new Error("Method not implemented.");
}
updata(type: T, id: number): boolean {
throw new Error("Method not implemented.");
}
delete(id: number): boolean {
throw new Error("Method not implemented.");
}
}
class use{
name:string | undefined;
password: string | undefined;
}
let people = new use()
people.name = '张三';
people.password = '123456';
let user = new mysqlDB<use>();// 类作为参数约束传入数据的类型
user.add(people)