分享好友 站长动态首页 网站导航

TypeScript基础知识梳理

网友发布 2022-10-26 20:41 · 头闻号编程技术

Typescript基础知识梳理

之前一直打算看看ts,但一直沉迷摸 无法自拔,公司目前项目中也没有ts项目。今年手上正好有两个小程序项目,于是打算用ts写一下,感受一下ts的“魅力”。

顺便整理了一下学习笔记,希望能帮到有需要的人。

同时也用XMind做了知识图谱,有需要源文件的可以留言。最后!记得点赞+关注支持一下。感谢支持 。

安装与编译安装npm install -g typescript //mac下安装前面需要加sudo编译tsc test.tsnpm install -g ts-node原始数据类型布尔值let isStatus:boolean=true;数字let isNumber:number=1;//16进制let isNumber16:number:=0b1010;字符串let isString:string='hello word';let year:number=2020;let add:string=`${isString},${year}!`;空值function alertName:void{ alert}let unusable: void = undefined;Null 和 Undefinedlet u:undefined=undefined;let n:null=null;let num:number=undefined;//不会报错let u:undefined;let num: number = u;//也不会报错let u:void;let num:number=u;// Type 'void' is not assignable to type 'number'.任意值什么是任意值类型let renyiString:string='string';renyiString=8;//Type 'number' is not assignable to type 'string'.let renyiString:any='string';renyiString=8;let anyThis:any='hello';console.logconsole.loglet anyThing: any = 'Tom';anyThing.setName;anyThing.setName.sayHello;anyThing.myName.setFirstName;未声明类型的变量let something;something = 'seven';something = 7;something.setName;let something: any;something = 'seven';something = 7;something.setName;数组的类型基础表示let numbers:number[]=[1,2,3,4,5]数组泛型let fibonacci:Array=[1,2,3,4,]let arr:[]=['tom',1];数组中对象类型的定义let arr:{name:string,age:number}[]=[{name:'tom',age:18}]type PeopleType={name:String,age:Number};let arr:PeopleType[]=[ {name:'bob',age:19}]class PeopleType{ name:string; age:number}let arr:PeopleType[]=[ {name:'bob',age:19}]元组的使用和类型约束let arr:[]=[111,'222',111];let arr:[number,string,number]=[111,'222',111];Interface接口const types==>{age>=20 && height>=180 && console.log;age<=20 && height <180 &&console.log};types //符合条件interface People{ name:string; age:number; height:number;}const types==>{ people.age>=20 && people.height>=180 && console.log; people.age<=20 && people.height <180 &&console.log};const choose==>{ console.log}const people={ name:'tom', age:18, height:178}types;choose;接口与类型别名的区别type People=string;interface People{ name:string; age:number; height:number;}interface People{ name:string; age:number; height:number; say :string};const types==>{ people.age>=20 && people.height>=180 && console.log; people.age<=20 && people.height <180 &&console.log};const people={ name:'tom', age:18, height:178}const people2={ name:'tom', age:18, height:178, say:'hello'}types;types;interface People{ name:string; age:number; height:number; say :string; [propname:string]:any;}const types==>{ people.age>=20 && people.height>=180 && console.log; people.age<=20 && people.height <180 &&console.log};const people={ name:'tom', age:18, height:178, say:'hello', add:'new add', addNumber:123}types;接口里的方法interface People{ name:string; age:number; height:number; goto:string;}const types==>{ people.age>=20 && people.height>=180 && console.log; people.age<=20 && people.height <180 &&console.log};const people={ name:'tom', age:18, height:178, goto{ return 'hello' }}types接口和类的约束interface People{ name:string; age:number; height:number; goto:string;}const types==>{ people.age>=20 && people.height>=180 && console.log; people.age<=20 && people.height <180 &&console.log};const people={ name:'tom', age:18, height:178, say:'hello', add:'new add', addNumber:123, goto{ return 'hello' }};class newPeople implements People{ name='bob'; age=19; height:190; say:'hello'; add:'new add2'; addNumber:123; goto{ return 'hi' }};let a=new newPeople;console.log)types接口的继承interface People{ name:string; age:number; height:number; say :string; [propname:string]:any; goto:string;}const types==>{ people.age>=20 && people.height>=180 && console.log; people.age<=20 && people.height <180 &&console.log};const people={ name:'tom', age:18, height:178, say:'hello', add:'new add', addNumber:123, goto{ return 'hello' }, back{ console.log }}interface newPeople extends People{ back:void}types类的基本使用class Test{ name='say' say{ return this.name }};class Test{ name='say' say{ return this.name }};class NewTest extends Test{ back{ return 'hello' }}const tom=new NewTest;console.log);console.log);super关键字的使用class Test{ name='say' say{ return this.name }};class NewTest extends Test{ back{ return 'hello' } say{ return super.say+'----hello' }}const tom=new NewTest;console.log);console.log);ts中类的访问类型class Person{ name:string}const person=new Person;person.name='tom';console.logpubilcclass Person{public name:string}const person=new Person;person.name='tom';console.logprivateclass Person{ private name:string; public say{ console.log; }}const person=new Person;person.name='tom';//报错console.log//报错protectedclass Person{ protected name:string; public say{ console.log; }}const person=new Person;person.name='tom';//报错console.log//报错class NewPerson extends Person{ public back{ console.log }}const newperson=new NewPerson;newperson.back//正常类的构造函数class Person{ pubilc name:string; constructor{ this.name=name }};const person=new Person;console.log;class Person{ constructor{}};const person=new Person;console.log;类继承中的构造器写法class Person{ constructor{}};class Teacher extends Person{ constructor{ super }};const teacher=new Teacher;console.log;ts中类的Getter、Setter、static以及readonlyclass Person{ constructor{ }}class Person{ constructor{ }; get age{ return this._age }};const person=new Person;console.logclass Person{ constructor{ }; get age{ return this._age-10 }};const person=new Person;console.logclass Person{ constructor{ }; get age{ return this._age } set age{ this._age=age }};const person=new Person;person.age=20;console.log//常规方法 class Person{ say{ return 'say hello' }};const person=new Person;console.log);class Person{static say{ return 'say hello' }};console.log)class Person{ constructor{}}let p=new Person;p.name='bob'//报错console.log;类的抽象类//错误演示abstract class Person { public abstract say}const tom=new Person//无法创建抽象类的实例//错误演示abstract class Person { abstract say{ console.log }}class Tom extends Person{ say{ console.log }};//正确方法abstract class Person { abstract say}class Tom extends Person{ say{ console.log }};tsconfig.json配置生成 tsc --init //终端执行编译//测试tsconfig.jsonconst test:string='tsconfig.json';tscinclude 、exclude 和 files"include":["text.ts"], "exclude":["text.ts"], "files":["text.ts"],compilerOptions配置removeCommentsstrictnoImplicitAny//这时候编译会报错function test { return name;}//正确 function test { return name;}strictNullChecks//此时就不会报错const test: string = null;outDir和rootDir{ "outDir": "./build" , "rootDir": "./src" ,}编译ES6语法"target":'es5' , // 这一项默认是开启的,你必须要保证它的开启,才能转换成功"allowJs":true, // 这个配置项的意思是联通sourceMapnoUnusedLocalsconst name:tring='111';export const age = "text";更多联合类型和类型保护联合类型interface Test{ name:string; say:=>{ }}interface Test2{ name:string; call:=>{};}function Tom{ console.log}function Tom{ fun.say}//报错类型保护-类型断言interface Test { text: boolean; say: void}interface Test2 { text: boolean; skill:string}function judgeWho { if { .skill; }else{ .say; }}const a={ text:true, skill:function:void{ console.log }, say:function:void{ console.log }}judgeWho //1类型保护-in语法interface Person{ name:string; say:=>{ }}interface Person2{ name:string; todo:=>{ }};function tom{ if{ val.todo }else{ val.say }}类型保护-typeof语法function add{ if{ return name+'---'+name2 }else{ return name+name2 }}add类型保护-instanecof语法class NumObject{ num:number}function numbers{ if{ return num1.num+num2.num }}Enum枚举类型function getName{ if{ return 'one' }else if{ return 'two' }else{ return 'three' }}console.log)const Status={ ONE:0, TWO:1, THREE:3}function getName{ if{ return 'one' }else if{ return 'two' }else{ return 'three' }}console.log);enum Status{ ONE, TWO, THREE}function getName{ if{ return 'one' }else if{ return 'two' }else{ return 'three' }}console.log);enum Status{ ONE=1, TWO,//2 THREE//3}enum Status{ ONE, TWO, THREE}function getName{ if{ return 'one' }else if{ return 'two' }else{ return 'three' }}console.log;泛型在函数中使用function add{ return `${one}${two}`}console.log)function add{ return `${one}${two}`}console.log)function add{ return `${one}${two}`}console.log) function add{ return `${one}${two}`}console.log)function add{ return `${one}${two}`}console.log)在类中使用class List { constructor {}; getItem:string{ return this.list[index] }};const list=new List;console.log)class List { constructor {}; getItem:string|number{ return this.list[index] }};const list=new List;console.log)class List{ constructor {}; getItem:T{ return this.list[index] }};const list=new List;console.log)const list=new List;interface People{ name:string}class List{ constructor {}; getItem:string{ return this.list[index].name }};const list=new List;console.log)泛型约束function list{ return `${name}`}console.log)class List { constructor { }; getItem:T{ return this.list[index] }}const list=new Listconsole.log)Namespace命名空间新建一个ts项目console.log;编写一个小组件class Header { constructor { const elem = document.createElement; elem.innerText = "This is Header"; document.body.appendChild; }}class Content { constructor { const elem = document.createElement; elem.innerText = "This is Content"; document.body.appendChild; }}class Footer { constructor { const elem = document.createElement; elem.innerText = "This is Footer"; document.body.appendChild; }}class Page { constructor { new Header; new Content; new Footer; }}命名空间
反对 0
打赏 0
更多相关文章

评论

0

收藏

点赞