当前位置: 首页 > Web前端 > JavaScript

TypeScript-基础理论

时间:2023-03-27 17:37:30 JavaScript

基础知识basictype:numberstringbooleanarrayobjectenumenumerationinterfacereturnsaboxofstatusfieldsenumActivityStatus{NOT_START='notStart',STARTED='stated'}conststatus=ActivityStatus.NOT_START;type,interface//typeUserInfo={//名称:字符串;//height?:number//}interfaceUserInfo{name:string;height?:number}constuserInfo={name:'cxx'}联合类型|(联合类型一次只能使用一种类型,交集类型每次为多个合并类型)交集类型&(联合类型一次只能使用一种类型,交集类型每次为多个合并类型)interfaceUserInfoA{名字?:字符串;高度?:number}interfaceUserInfoB{width:number}functiontest(param:UserInfoA|UserInfoB){//...}typeoftypeof'a'//stringfunctiontoArray(x:number):Array{返回{x};}typeFunc=typeoftoArray;//(x:number)=>number[]keyof//可以用来获取一个对象接口Person中的所有键值Person{name:string;年龄:数字;}输入KPerson=人的关键;//'名字'|'age'in用于遍历枚举类型typeKeys='a'|'b'|'C';typeobj={[键中的键]:任何;}extendsinheritancetypeinterfaceTLength{length:number}functionloggingIdentity(arg:T):T{console.log(arg.length);返回参数;}loggingIdentity(3);loggingIdentity({length:10,value:3});ParitialParitial是让某个类型的所有属性都可选interfacePageInfo{title:string;}typeOptionalPageInfo=Paritial;Required全部变为强制Readonly全部变为只读interfacePageInfo{title:string;}类型ReadonlyPageInfo=Readonly;constpageInfo:ReadonlyPageInfo={title:'cxx'};pageInfo.title='ch';//errorRecordRecord将所有属性的值转换为T类型interfacePageInfo{title:string;}输入Page="home"|“关于”|“接触”;constx:Record={home:{title:'111'},about:{title:'222'},contact:{title:'333'},}Exclude排除删除属于另一种类型的一种类型T0=Exclude<"a"|“乙”|"c","a">//"b|"c"typeT1=Exclude<"a"|"b"|"c","a"|"b">//"c"ExtractExtract从T中提取U,可能意味着交集类型T0=Extract<"a"|"b"|"c","a"|"f">//"a"typeT1=Extract无效),函数>//()=>无效