当前位置:网站首页>Typescript syntax

Typescript syntax

2022-06-24 21:06:00 AcTarjan

data type

any 	 Any data type 		let x : any = "nihao"; x = 12; 
number 	 Double precision 64 Bit floating point value  			0b1011 0o744 12 0xa1 3.14
string 	 character string , Backquotes support inline expressions 	'nihao' "nihao" `nihao ${
       name }`
boolean  Boolean value 	false true
 Array 		let arr: Array<number> = [1, 2]; let arr : number[] = [1,2];
 Tuples      let x : [string,number] = ["AcTarjan",21];
enum 	 enumeration 		enum Color {
    Red,Blue,White}; let c : Color = Color.Red
null	 Missing object value 
undefined		 Default value of uninitialized variable 
never	 Other types ( Include  null  and  undefined) Subtypes of , Represents a value that never appears 

Variable

  • Naming specification : It can contain numbers 、 Letter 、_ or $; Cannot start with a number
  • var and let Can be used to declare variables ,var The scope of is the function scope ,let The scope of is a block level scope , priority of use let
// Variable declarations 
let [ Variable name ] : [ Variable type ] =  value ;
let [ Variable name ] : [ Variable type ];		// The value is undefined
let [ Variable name ] =  value ;				// Variables can be of any type 
let [ Variable name ];					// Variables can be of any type   The value is undefined

// Constant declaration 
const [ Constant names ] : [ constant type ] =  value ;
const [ Constant names ] =  value ;				// Constants are of any type 

// Variable type assertion ( Similar to type conversion )
< type > value 				
 value  as  type 
let num : number = <number>'12' 
let num : number = '12' as number 			

Process control

// Conditional statements 
if () {
    
}
if () {
    
} else {
    
}
if () {
    
} else if () {
    
}
switch (name) {
    
    case 'AcTarjan' :
       console.log('AcTarjan');
       break; /*  Optional  */
    case 'Bob':
       console.log('Bob');
       break; /*  Optional  */
    default : /*  Optional  */
      console.log('default');
}

// Loop statement 
var i : number
for ( i = 0; i < 100; i++ ) {
    
    console.log(i)
}
//for...of  Allow you to traverse  Arrays( Array ), Strings( character string ), Maps( mapping ), Sets( aggregate ) Such as iterative data structure, etc .
for (let val of list) {
    
	console.log(val)
	continue;
}
while () {
    
	break;
}
do {
    
} while();

function

// General functions 
function add(x: number, y: number): number {
    
    return x + y;
}

// Optional parameter function 
function buildName(firstName: string, lastName?: string) {
    
    if (lastName)
        return firstName + " " + lastName;
    else
        return firstName;
}

// Parameter default function 
function calculate_discount(price:number,rate:number = 0.50) {
     
    var discount = price * rate; 
    console.log(" The result of the calculation is : ",discount); 
} 

// Residual parameter function 
function buildName(firstName: string, ...restOfName: string[]) {
    
    return firstName + " " + restOfName.join(" ");
}

// Anonymous functions 
var msg = function() {
     
    return "hello world";  
} 
console.log(msg())

//Lambda  function 
var foo = (x :number) => {
    
	return 10 + x 
}
console.log(foo(100))      // The output is  110

Classes and objects

// Class inheritance keyword : extends
// Interface implementation keywords : implements
class Car {
     
    //  Field  
    engine :string;
    static num :number; 
 
    //  Constructors  
    constructor(engine :string) {
     
        this.engine = engine 
    }  
 
    //  Method  
    disp():void {
     
        console.log(" The engine is  : "+this.engine) 
    } 
}
var obj = new Car("Engine 1");
var x = obj instanceof Car		// x = true  Judge obj Whether it is Car The object of 

interface ILoan {
     
   interest :number 
} 
原网站

版权声明
本文为[AcTarjan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211319438175.html