TypeScript entry-six: TypeScript generic

  • Generic function
  • Generic class

 First, the generic function

Before generic function, briefly describe what generics, generics can be defined as a variable to determine its type when using variables. What does that mean? If there is now a function, a phenomenon that may arise parameters and return values ​​occur in many cases, only when you call the function parameters can be determined by their type, you can define a function as a generic function, and then call this function when setting parameters and return types.

1 function identity<T>(arg: T): T {
2     return arg;
3 }
4 console.log(identity<number[]>([1,2,3]));//[1,2,3]
5 console.log(identity<string>('aaa'));//aaa

Generics-related content is relatively simple, the official documentation is very detailed, here is not to do too much analysis, we show some basic examples for various applications:

1  // can write (using the parameters and return values of the generic type array type) 
2  function Fun <T> (Arg: T []): T [] {
 . 3    the console.log (arg.length);
 . 4    return Arg ;
 5  }
 6  // may also be such that (generic type parameters, return value generic type of array type) 
. 7  function fun1 <T> (Arg: T): T [] {
 . 8    return [];
 . 9  }
 10  // However, not write 
. 11  // function fun2 <T> (Arg: T []): {T 
12 is  //    return arg.length; 
13 is  // }

Generic functions can also be used as a generic function type:

1 function identity<T>(arg: T): T{ 
2   return arg;
3 }
4 let myIdentity: <U>(arg: U) => U = identity;

It should be very easy to think of the above generic function type, is it possible to define a generic interface? This is certainly not a problem:

1  // define a generic function interface 
2 interface GenericIdentityFn <T> {
 . 3    (Arg: T): T;
 . 4  }
 . 5 interface GenericIdentityFn <T> {
 . 6    (Arg: T): T;
 . 7  }
 . 8  // Here pan- type process can be written directly to the generic function implemented interface 
9  // here as a generic function type 
10  // implemented with this type of generic functions as a variable function of a generic function interface 
. 11  function Identity <T> (Arg : T): {T 
 12 is    return Arg;
 13 is  }
 14  // implement generic function interface 
15 let myGenericIdentityfn : GenericIdentityFn<string | number | number[]> =  identity;
16 identity('str');

Then, the above comments also forget it, can pass a plurality of the generic type, followed by generic interface implemented or (|) operator connection.

 Second, the generic class

Generic class and generic functions are very similar, there is not much to say, directly on the code, the first example of official documents written only as an abstract class or interface the same content, so in order to correct the test, you need Code completion, here is my code completion:

 1 class GenericNumber<T> {
 2   zeroValue: T;
 3   constructor(zero:T){
 4     this.zeroValue = zero;
 5   }
 6   add(x: T, y: T):T[]{
 7     return [x,y];
 8   }
 9 }
10 
11 let myGenericNumber = new GenericNumber<number>(0);
12 myGenericNumber.zeroValue = 100;
13 myGenericNumber.add(10,100);

Use of available generic class interface to solve the problem can not be described in the internal data types as a generic function, but also provides sample code in the official document:

1 interface Lengthwise {
2   length: number;
3 }
4 
5 function loggingIdentity<T extends Lengthwise>(arg: T): T {
6   console.log(arg.length);  // Now we know it has a .length property, so no more error
7   return arg;
8 }
9 console.log( loggingIdentity({length:10,value:3}) );

When the generic class interface also known as generic constraints, simply means that a given class type interface, and then create a generic method or the type of time inherit this interface, and then call the function or class is instantiated according to the type of interface type, transmission the type corresponding to the value of the generic type as:

. 1  interface G {
 2    objG: Object;
 . 3    keyG: String;
 . 4  }
 . 5  function getProperty <T G the extends> (obj: T [ 'objG'], Key: T [ 'keyG' ]) {
 . 6    // return obj [ key] - is uncertain since the key is no way to deduce its type, it can not be implemented 
. 7    return obj
 . 8  }
 . 9  
10 the let X = {a:. 1, B: 2, C:. 3, D:. 4 } ;
 . 11  
12 is the console.log (getProperty (X, "A")); // {A:. 1, B: 2, C:. 3, D:}. 4

Used in a generic types: simply means that the specified type is a generic class, so the type specified by this parameter or a return value of the object instance of that class.

1  // Note that this does not initialize variables assigned to an official member exemplary class definition, assignment structure is not implemented, all class members will be given 
2  class {Beekeeper
 . 3    hasMask: Boolean ;
 . 4  }
 . 5  class the ZooKeeper {
 . 6    nameTag: String ;
 . 7  }
 . 8  class Animal {
 . 9    numLegs: Number;
 10  }
 . 11  class the extends Animal {Bee
 12 is    Keeper: Beekeeper;
 13 is  }
 14  class the extends Lion Animal {
 15    Keeper: the ZooKeeper;
 16  }
 . 17  function the createInstance <A Animal> the extends (C : new new () => A): A {
18   return new c();
19 }
20 createInstance(Lion).keeper.nametag;  // typechecks!
21 createInstance(Bee).keeper.hasMask;   // typechecks!

Guess you like

Origin www.cnblogs.com/ZheOneAndOnly/p/11780553.html