TypeScript practice is essential to note (4) - Generic

  Generic programming language is a style or paradigm, equivalent to the type template, type in the time allowed to ignore the statement and other members of the class, interface or function, and then specify the type of use in the future, its main purpose is to provide for them there significant constraints to enhance the reusability of code.

First, the generic parameter

  When a function needs to be able to handle various types of parameters and return values, and have constraints between them (e.g. to the same type), generic syntax can be employed, as shown below.

function send<T>(data: T): T {
  return data;
}

  Function name followed by a <T>, where T is called the generic type parameters or variables, represent a data type. Note that, T is only a placeholder, containing more semantics that can be named, for example TKey, TValue like. In use, both the specified type, the type can be automatically determined as follows using the type inference.

Send <Number> (10);         // specified type 
Send (10);              // type inference

  When you need to deal with an array of type T, you could write like this.

function send<T>(data: T[]): T[] {
  return data;
}
send<number>([1, 2, 3]);

  When specifying a type of generic functions, parameters need to include the generic names as shown below, wherein the generic parameters and function parameters can be defined in different time.

let func: <U>(data: U) => U = send;

  Also supports a plurality of generic parameters passed, simply to increase the placeholder upon the type of statement. In the following example, T and U will be merged into a tuple type, there are many other uses, will be explained later.

function send<T, U>(data: [T, U]): [T, U] {
  return data;
}
send<number, string>([1, "a"]);

Second, generic interface

  In the interface, may be utilized to constrain a generic structure of the function, the following call signature, the interface contains generic parameters declared.

interface Func {
  <T>(str: T): T;
}
function send<T>(str: T): T {
  return str;
}
let fn: Func = send;

  After the generic parameters may also exist as a parameter interface, i.e., the wrap angle brackets with the generic parameter moved interface name, as shown below.

interface Func<T> {
  (str: T): T;
}
function send<T>(str: T): T {
  return str;
}
let fn: Func<string> = send;

  When the Func interface as the type used, the type of a required pass thereto, for example, the above assignment statement string.

Third, the generic class

  Similar generic interface with generic class, is added after the name of the generic parameters, as shown below, wherein the send attribute "=>" symbol is not an arrow indicating function, but to define the type of the return value.

class Person<T> {
  name: T;
  send: (data: T) => T;
}

  When generic class instance, you need to specify a type, as shown below.

let person = new Person<string>();
person.send = function(data) {
  return data;
}

  Note that the static part, the class can not use the generic parameters.

Fourth, generic constraints

  When using generic, in advance since the data type of the parameter is not clear, it can not be arbitrarily invoked properties or methods, their use can not even operator. In the following examples, the access of the data length attribute, but the compiler can not determine its type, and therefore will be given.

function send<T>(data: T): T {
  console.log(data.length);
  return data;
}

  TypeScript allows the addition of a generic constraint parameters to be able to call the appropriate property or method, as shown below, by the extends keyword string constraint T must subtype.

function send<T extends string>(data: T): T {
  console.log(data.length);
  return data;
}

  After the addition of the constraint, Send () function can not receive the digital parameters of the type, as shown below.

the send ( "10");         // correct 
the send (10);           // error

Example 1) Create a class

  When using the generic class factory to create a function declaration type T has required the constructor, as shown below.

class Programmer { }
function create<T>(ctor: {new(): T}): T {
  return new ctor();
}
create(Programmer);

  With "{new (): T}" replace the original type of placeholder to indicate new operator may be instantiated, and T type is obtained, as shown in another writing the same effect as follows.

function create<T>(ctor: new()=>T): T {
  return new ctor();
}

2) a plurality of generic parameters

  In the TypeScript, among a plurality of generic parameters may be bound to each other, as shown, creating a base class and a derived class Person Programmer, and create () T is a constraint function of U subtype.

class Person { }
class Programmer extends Person { }
function create<T extends U, U>(target: T, source: U): T {
  return target;
}

  When passed to create () function parameters does not meet the constraints, it will be given at compile time, as shown below.

the Create (Programmer, the Person);         // correct 
the Create (Programmer, 10);             // error

 

Guess you like

Origin www.cnblogs.com/strick/p/11697909.html