当前位置:网站首页>Basic data type and structure data type of TS

Basic data type and structure data type of TS

2022-06-21 18:27:00 CupidoZ

TS Basic data type and structure data type of

// js Primitive types 

let num:number = 5000;
let str:string = ' Hello , I'm a string ';
let bool : boolean = false;
let nul = null;
let undef;
let sym : symbol =Symbol('tag_info');
let obj_stu  = {
    
    id:'10086',
    name:'cupido',
    hobby:'swim'
}

let nums:number[] = [1,2,3,5,8,7]
let strs:Array<string> = ['a','ccc','sad','affad','123486']

// ts New inspection type 
//  Joint type 
//  Yes () Represents a mixed array 
let arr : (number|string|boolean)[] = ['sad',12345,'haha',89898.2,'aa',false,true];
//  No, () To express or relate to   Can only be one of the specified types 
let arr_or : number|string|boolean[] = [false,true];

//  Type the alias 
//  Customize the data structure of the interface type   Avoid declaring types repeatedly when reusing 
type Mix_num_str = (number|string)[];
let arr_mix : Mix_num_str = [45654," ha-ha ",'asdafbjh',45564688,2.2,'45']
let arr_mixdemo:Mix_num_str=[11111,22222,'haha']

//  Function type 
 function plus(a:number, b:number):number{
    
         return a+b;
}
console.log(plus(1,2));

 const add = (a:number,b:number):number=>{
    
     return a+b;
 }
 console.log(add(1,2));

 const add_obj :(a:number,b:number)=>number=(a,b)=>{
    return a+b;};

//  Empty type 
function none(a:number, b:number):void{
    
    console.log(' Functions with no return value ')
}

//  Optional parameters  
//  Similar to variable parameters   You can pass it on   You can also pass any number of specified parameter types 
function Slice(start?:number,end?:number):number{
    
    return start+end;
}
Slice()
Slice(1)
Slice(1,2)


//  Object type qualification 
//  Type declaration 
var Person1 :{
    id:number;name:string;gender:boolean;SayHi(name:string):void}={
    
    id: 1001,
    name: "cupido",
    gender: false,
    SayHi: function (name): void {
    
        throw new Error("Function not implemented.");
    }
}
//  Optional attribute ?
var Person2 :{
    id:number;name:string;gender?:boolean;SayHi(name:string):void}={
    
    id: 1001,
    name: "cupido",
    SayHi: function (name): void {
    
        throw new Error("Function not implemented.");
    }
}

//  Interface type 
interface IPerson{
    id:number;name:string;gender?:boolean;SayHi(name:string):void}

let person_obj : IPerson = {
    
    id: 0,
    name: "Lulu",
    //  Optional attribute 
    gender:true,
    SayHi: function (name: string): void {
    
        throw new Error("Function not implemented.");
    }
}


//  Interface inheritance 
interface Point2D{
    x:number;y:number}

interface Point3D extends Point2D{
    z:number}

let point1:Point3D={
    
    z: 0,
    x: 0,
    y: 0
}


//  A tuple type tuple
//  You can specify the element type and the number of parameters to check 
let location_obj:[number,number]=[25.65,66.33]



//  Type inference 
let name1;
// name1=22 No error is reported at this time 
let age = 18;
// age = 'a'  When the variable type is not specified   Type inference specifies the corresponding data type of variable declaration after variable initialization   Change the variable type again and an error is reported 



//  Types of assertions 
//  Assertion keyword not used as when  alink Parent class of HtmlElement There is no href attribute   When we know alink The type of is a subclass HTMLAnchorElement when 
//  Can be done as Type designation   So you can get subtypes HTMLAnchorElement Under the href attribute 
//  You can view the check information of the console for the parent class attribute  a Labeled _protoType_ The type of 
const alink = document.getElementById('alink') as HTMLAnchorElement
alink.href=='http://www......'



//  Literal type 
// str02 The type is 'cupido'  Because it is a constant enumeration type   So there will be unique types   So strings can also appear as types   But the values can only be the same 
//  This type of data can be understood as an enumeration 
let str01 = 'cupido';
const str02 = 'cupido';
let str03:'cupido'='cupido';

function ChangeDirection1(derection:'up'|"down"|'left'|'right'){
    
        console.log(' The direction of my movement is :'+derection)
}
//  The corresponding constant is specified when calling 
ChangeDirection1('up')


//  Enumeration type 
//  If the enumeration member in the enumeration is not initialized   There will be a default value and the default value will increase automatically   from 0----1,1++.....
enum Derections{
    
    UP = 'up',
    DOWN='down',
    LEFT='left',
    RIGHT='right' 
}
function ChangeDirection2(derection:Derections){
    
    console.log(' The direction of my movement is :'+Derections)
}
//  The corresponding constant is specified when calling 
ChangeDirection2(Derections.UP)



// any type 
//  Use any When it comes to type   Object will lose its type checking mechanism   You can modify the behavior at will 
let num_obj:any = 'haha';
num_obj=123456789;

let any_OBJ = {
    
    id:123456,
    maker:'xiaomi',
    person:Person1,
    date:new Date()
}


// typeof()
console.log(typeof(num_obj))
//  It can be used for parameter type checking 
function sayHI(name:typeof Person1): void {
    
    throw new Error("Function not implemented.");
}



原网站

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