A brief description of common methods in TypeScript

[A] brief description of common methods in @TypeScript


Define a simple map consisting of key-value.

type   hero= { 
      age : number , 
     name : string 
Construct a map to easily load hero data 
public  heroMap:Record<number,hero>


Array.filter() is a built-in TypeScript function that creates a new array.

built-in methods;
filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
Instructions for use:
array.filter(predicate, thisArg)
Parameters: This method accepts two parameters mentioned and described below:
predicate: This parameter is the function to test each element.
thisArg: This parameter is the Object used as this object when executing the callback .
type item={
//Class 1 
let class1: Array <item>=[{ age : 10 , name : "First Class No. 1" },{ age : 8 , name : "First Class No. 2" },{ age : 12 , name : "No. 3 of the first class" },{ age : 16 , name : "No. 4 of the first class" }];
 //Class 2 
let class2: Array <item>=[{ age : 10 , name : "No. 1 of the second class" },{ age : 8, name : "Class 2 No. 2" },{ age : 12 , name : "Class 2 No. 3" }];
 //Find class 1 students who are older than 10 years old 
let check1=class1.filter( _ => _. age> 10 );
 console .log( "check1" ,check1);
 //find class 1 who is younger than class 3 classmate 3 
let check2=class1.filter( ( item )=> {
     let mate2= class2[ 2 ];
     return item.age<mate2.age;


The map() method returns a new array consisting of the values ​​returned by calling a specified method for each element in the original array.

map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
callbackfn Returns a new element after the elements in the original array pass through this methodthisArg The object pointed to by this 
when the thisArg function is executed .
type newBody={
//Return the new class structure 
//The type of class3 at this time is newBody[] 
let class3 = class1.map( _ => {
     let str:newBody={
         age :_.age,
         name :_.name,
         school : "Hope Primary School"
    return str;
console .log( "class3" ,class3);
 //Take out the age as a new array 
//The type of class4 at this time is number[] 
let class4 = class1.map( _ => {
     let str=_.age;
     return str;


The some() method tests whether some elements in the array pass the test of the specified function.

some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
Function to test each element.
thisArgThis value 
to use when executing the callback .
// Find students who are older than 11 years old, and end the traversal if found
let result1= class1.some((_)=>{
        return true


The Object.assign() method is used to copy the enumerable property values ​​of one or more source objects to the target object, and the return value is the target object.

Object.assign(target,  ...sources) 
target : target object 
sources : source object 
type  type1={ 
    age : number, 
    name : string, 
    six : string, 
type  type2={ 
    age : number, 
    height : number 
let  grad1 :type1={age:1,name:"lila",six:"male"} 
let  grad2:type2={age:1,height:100} 
//Merge two types of data  bind type is type1 & type2 
let  bind =Object.assign(grad1,grad2); 


The new string method includes() method in es6 simplifies the judgment. indexOf() returns the position of the containing string. If == -1, it means that the string is not included, and the includes() method returns are boolean values, i.e. true and false


Equivalent to the function that calculates the accumulation, the callback of the .reduce() method requires two parameters: the accumulator and the current value. The accumulator will be the first parameter, the “pass down” value.

reduce(callbackfn: ( previousValue: T, currentValue: T, currentIndex: number, array: T[] ) => T, initialValue : T): T;
 let age = [ 10 , 14 , 15 , 17 , 20 ];
 / / Calculate the sum of age, 0 (pass in value) + 10 + 14....... 
let total = age .reduce( ( total,donation )=> {
 return total + donation;
}, 0);

generic T

 // Build a method that can return any desired type 
// If test<T> is changed to test< T extends boolean|number> Here is a certain restriction on the incoming type 
async test<T>(){
     return  new  Promise < T> ( (res,rej )=> {
         let x : T;
  async test1(){
       //At this time, return whatever type is passed 
        let y = await  this .test<boolean>(); //return boolean 
        let y2 = await  this .test<number>(); //return number

Leave a Comment

Your email address will not be published. Required fields are marked *