Typescript  
一、Javascript 
TypeScript是JavaScript的超集,它扩展了JavaScript的语法和功能,同时增加了类型系统和其他工具,使得代码更加可读性和可维护性。
text::JavaScript 
 
 
二、补充 
对Js文章的补充,其中有一些不一定是Ts的特性。
 
1 限定类型 
Ts特性。
 
const  url : string  = "https://www.baidu.com" ;const  sex : "male"  | "female" ;addData (data : number ): void {}
 
2 限定修饰符 
Ts特性。
public表示公共变量,可以被任何地方访问。
private表示私有变量,只能在类内部访问,不能被子类或实例访问。
protected表示受保护变量,只能在类内部和子类中访问,不能被实例访问。
 
class  Person  {    public  name : string ;      private  age : number ;      protected  gender : 'male'  | 'female' ;      constructor (name: string , age: number , gender: 'male'  | 'female'  ) {         this .name  = name;         this .age  = age;         this .gender  = gender;   } } class  Student  extends  Person  {    constructor (name: string , age: number , gender: 'male'  | 'female' , public  studentId: string  ) {         super (name, age, gender);   }     public  sayHello ( ) {         console .log (`Hello, my name is ${this .name} , and my student ID is ${this .studentId} .` );                  console .log (`My gender is ${this .gender} ` );      } } const  student1 = new  Student ('Alice' , 20 , 'female' , '001' );console .log (student1.name ); console .log (student1.studentId ); class  MathUtils  {    static  PI  = 3.14 ;     static  add (a: number , b: number  ) {     	return  a + b;     }          static  multiply (a: number , b: number  ) {         return  a * b;     }     static  calculateCircumference (radius: number  ) {         return  2  * this .PI  * radius;     } } console .log (MathUtils .add (1 , 2 )); console .log (MathUtils .multiply (3 , 4 )); console .log (MathUtils .calculateCircumference (2 )); 
 
3 函数重载 
Ts特性。
在Js中后面的定义的函数会覆盖掉前面的函数。
 
function  add (a: number , b: number  ): number ;function  add (a: string , b: string  ): string ;function  add (a: any , b: any  ): any  {  return  a + b; } console .log (add (1 , 2 ));           console .log (add ("hello" , "world" )); 
 
4 接口 
Js、Ts中都存在。
接口中的内容都要在类中实现。
如果有不确定的属性要实现,可以在属性名后加一个?,这样它就不一定要被实现。
 
interface  Person  {  name : string ;   age : number ;   gender : 'male'  | 'female'  | 'other' ;   sayHello (): void ; } class  Student  implements  Person  {    name : string ;     age : number ;     gender : 'male'  | 'female'  | 'other' ;     studentId : string ;          constructor (name: string , age: number , gender: 'male'  | 'female'  | 'other' , studentId: string  ) {         this .name  = name;         this .age  = age;         this .gender  = gender;         this .studentId  = studentId;   }     sayHello ( ) {     	console .log (`Hello, my name is ${this .name} , and my student ID is ${this .studentId} .` );   } } const  student1 = new  Student ('Alice' , 20 , 'female' , '001' );student1.sayHello ();  
 
5 断言 
Ts特性。
明确告诉编译器某个类型存在
 
const  button = document .querySelector ('button' ) as  HTMLButtonElement ;
 
6 异步 
Js、Ts中都存在。
 
 
function  getData ( ): Promise <string > {  return  new  Promise ((resolve, reject ) =>  {     setTimeout (() =>  {       resolve ("Hello, world!" );     }, 2000 );   }); } async  function  main ( ) {  console .log ("Start" );   try  {     const  result = await  getData ();     console .log (result);   } catch  (error) {     console .error ("Error: " , error);   }   console .log ("End" ); } main ();
 
7 泛型 
Ts特性。
T表示任意类型,用尖括号包裹,使用时需要补充类型。
 
identity<T>(arg : T): T {     return  arg; } console .log (this .identity <Number >(123 ));console .log (this .identity <string >("hello" ));