let a: number;
let b: string;
a=4;
b='吃饭'
let arr: Array<number>=[3,4]
let arrc: string[]=['吃饭']
//使用接口来定义对象
interface ObjT{
name: string;
age: number;
}
let infor: ObjT={
name:'储',
age:24
}
//接口设置可选项(就是可写可不写)
interface fv{
name?:string;
age:number
}
let hb: fv={
age:14
}
//继承单个
interface Obj2 extends ObjT {
class: string
}
let infor1: Obj2={
name:'储',
age:24 ,
class:'五班'
}
//继承多个
interface All extends ObjT,Obj2{
ter: Array<number>
}
let allc: All={
name:'储',
age:24 ,
class:'五班',
ter:[1,3]
}
//ts中的与 相当与all继承了ObjT Obj2
let yu: ObjT & Obj2 & All={
name:'储',
age:24 ,
class:'五班',
ter:[1,3]
}
//ts中的或
let test: number | string | Array<number>=2
//不确定的属性
interface IP1{
[propName: string]:any
}
// let demo1:IP1={} //有没有属性都可以
let demo1:IP1={
name:'是各个'
}
//implements接口实现
interface IP2{
name: string;
age: number;
sex: string;
getName:()=>string;
getAge ():number;//两种函数写法都可以
}
class Person implements IP2{
name: string='小储';
age: number=18;
sex: string='nv';
getName ():string{
return this.name
};
getAge(): number {
return this.age
};
//类自己的方法
hell(): void{
}
}
//类型断言
function getLength (str: number | string): number{
if((str as string).length){
return (str as string).length
}else{
return str.toString().length
}
}
//非空断言
function getL (arr: string): number{
return arr!.length
}
//泛型
function fun1<T>(arg: T): void{
}
fun1<number>(123)