Typescript knowledge --- (6) generic

1. What is a generic?
 
Data types are not pre-determined, the specific type can be determined at the time of use
function log <T> (value: T) {
   return value; 
} 

// Called 
log <String []> ([ 'A', 'B' ]) 

// may also be omitted according to the type of parameter type inference 
log ( [ 'a', 'b' ])

 

If any types to define this function, it means that the function passed parameters and return values ​​may be of any type, for example: a string type passed, it is returned numeric
function log(value: any): any {
  return value;
}

 

2, generic function type
function identity<T>(arg: T): T {
  return arg;
}

let myIdentity: <T>(arg: T) => T = identity;

 

3, generic interface
{Log1 interface
   <T> (value: T): T    // use a generic constraint function 
} 

interface Log2 <T> {   // all members of a generic interface constraints 
  (value: T): T 
} 

function log <T > (value: T): T {
   return value; 
} 

// when implementing the generic interface must specify the type of 
the let myLog: Log2 <Number> = log; 
myLog ( 2 ); 

// if not specified type, may be generic interface to specify a default type 
interface log3 <String T => { 
  (value: T): T 
} 

the let myLog3: log3 = log; 
myLog3 ( 'Hello');

 

4, generic class
 
Examples of members of the generic class can not be used static member constraints, the only constraint class
the Log class <T> { 
  RUN (value: T): T { 
    return value 
  } 

  // static foo (value: T): {T} // return value can not refer to a static class member Parameter Type 
} 

the let that log1 = new new the Log <Number > (); 
log1.run ( 2 ); 

the let Iog2 = new new the Log (); 
log2.run ( 'Hello');

 

5, generic constraints
 
Log function in the example above, if we want to access its length property function parameters, the program will complain, because not all types have the length property
function log <T> (value: T): T { 
  the console.log (value.length);    // type attribute "length" does not exist on the "T" 
  return value; 
}
 
To this end, we want to list the constraints of the requirements on T, the incoming type must have the property right. An interface may be described by defining constraints, the use of this interface and the extends keyword to implement the constraints
{HasLength interface 
  length: Number; 
} 

function log <HasLength the extends T> (value: T): T { 
  the console.log (value.length);    
  return value; 
} 

log ([ . 1]);    // . 1 
log ( 'Hello ; ')    // . 5 
);: log (length {2}    // 2 
; log (2)    // parameter type "2" can not be assigned to the parameter type "HasLength" of

 

6, the benefits of using generics

(1), scalability enhancement procedures: function or class can easily support multiple types of data

(2) enhance the readability of the code: do not write more than function overloading, or type a lengthy joint statement

(3), a flexible control of the type of constraint between

Guess you like

Origin www.cnblogs.com/rogerwu/p/12201555.html