typescript泛型(学习笔记非干货)

软件工程中,咱们不只要建立一致的定义良好的API,同时也要考虑可重用性。 组件不只可以支持当前的数据类型,同时也能支持将来的数据类型, 这在建立大型系统时为你提供了十分灵活的功能。 In software engineering, we should not only create well-defined APIs, but also consider reusability. Components can support not only current data types, but also future data types.This provides you with very flexible functionality when creating large systems. 在像C#和Java这样的语言中,能够使用泛型来建立可重用的组件,一个组件能够支持多种类型的数据。 这样用户就能够以本身的数据类型来使用组件。 In languages like C# and Java, generics can be used to create reusable components, and a component can support multiple types of data. This allows users to use components with their own data types.ide

泛型之Generic Generic Generic identity函数。 这个函数会返回任何传入它的值。 你能够把这个函数当成是echo命令。 identity Function. This function returns any value passed in. You can think of this function as echo command. 不用泛型的话,这个函数多是下面这样:函数

Without generics, this function might be as follows:
function identity(arg:number):number{
    return arg;
}
function identity(arg:any):any{
    return arg;//传入的类型与返回的类型应该是相同的。 
}

咱们须要一种方法使返回值的类型与传入参数的类型是相同的。 这里,咱们使用了 类型变量,它是一种特殊的变量,只用于表示类型而不是值。 We need a way to make the type of the return value the same as the type of the incoming parameter. Here, we use a type variable, which is a special variable and is used only to represent a type rather than a value.flex

function identity<T>(arg:T):T{
    return arg;
}

咱们定义了泛型函数后,能够用两种方法使用。 第一种是,传入全部的参数,包含类型参数: After we define generic functions, we can use them in two ways. The first is to pass in all parameters, including type parameters:this

let output=identity<string>("myString");//string

第二种方法更广泛。利用了类型推论 -- 即编译器会根据传入的参数自动地帮助咱们肯定T的类型: The second method is more common. Using type inference, the compiler automatically helps us determine the type of T based on the parameters passed in: let output=identity("mySomething");//string 使用泛型变量 Using generic variablesspa

function identity<T>(arg:T):T{
    return arg;
}

若是咱们想同时打印出arg的长度。 咱们极可能会这样作: If we want to print the length of Arg at the same time. We are likely to do so:prototype

function identity<T>(arg:T[]):T[]{
    console.log(arg.length);
    return arg;
}
function loggingIdentity<T>(arg:Array<T>):Array<T>{
    console.log(arg.length);
    return arg;
}

泛型类型 generic typescode

function identity<T>(arg:T):T{
    return arg;
}
let myIdentity:<T>(arg:T)=>T=identity;

咱们也能够使用不一样的泛型参数名,只要在数量上和使用方式上能对应上就能够。 We can also use different generic parameter names as long as they correspond quantitatively and in terms of usage.component

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

咱们还能够使用带有调用签名的对象字面量来定义泛型函数: We can also use object literals with call signatures to define generic functions:对象

function identity<T>(arg:T):T{
    return arg;
}
let myIdentity:{<T>(arg:T):T}=identity;

这引导咱们去写第一个泛型接口了。 咱们把上面例子里的对象字面量拿出来作为一个接口: This leads us to write the first generic interface. Let's take the literal quantities of the objects in the example above as an interface:接口

interface GenericIdentityFn{
    <T>(arg:T):T;
}
function identity<T>(arg:T):T{
    return arg;
}
let myIdentity:GenericIdentityFn=identity;

一个类似的例子,咱们可能想把泛型参数看成整个接口的一个参数。 这样咱们就能清楚的知道使用的具体是哪一个泛型类型。 For a similar example, we might want to consider generic parameters as a parameter of the entire interface. In this way, we can clearly know which generic type we are using. 这样接口里的其它成员也能知道这个参数的类型了。 So that other members of the interface can also know the type of this parameter.

interface GenericIdentityFn<T>{
    (arg:T):T;
}
function identity<T>(arg:T):T{
    return arg;
}
let myIdentity:GeneratorFunction<number>=identity;//把非泛型函数签名做为泛型类型一部分

除了泛型接口,咱们还能够建立泛型类。 注意,没法建立泛型枚举和泛型命名空间。 In addition to generic interfaces, we can also create generic classes. Note that generic enumerations and generic namespaces cannot be created. 泛型类 generic class

class GenericNumber<T>{
    zeroValue:T;
    add:(x:T,y:T)=>T;
}
let myGenericNumber=new GenericNumber<number>();
myGenericNumber.zeroValue=0;
myGenericNumber.add=function(x,y){return x+y;};

与接口同样,直接把泛型类型放在类后面,能够帮助咱们确认类的全部属性都在使用相同的类型。 Just like interfaces, placing generic types directly behind classes can help us confirm that all attributes of classes use the same type. 泛型约束 Generic constraints

function loggingIdentity<T>(arg:T):T{
    console.log(arg.length);
    return arg;
}

咱们定义一个接口来描述约束条件。 建立一个包含 .length属性的接口,使用这个接口和extends关键字还实现约束: We define an interface to describe constraints. Create an interface with. length attributes, and use this interface and extends keywords to implement constraints:

interface Lengthwise{
    length:number;
}
function loggingIdentity<T extends Lengthwise>:T{
    console.log(arg.length);
    return arg;
}

在泛型约束中使用类型参数 Using type parameters in generic constraints 你能够声明一个类型参数,且它被另外一个类型参数所约束。好比, You can declare a type parameter and it is constrained by another type parameter. For example,

function find<T,U extends Findable>>(n:Text,s:U){

}
find(giraffe,myAnimals);

在泛型里使用类类型 Use class types in generics

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

使用原型属性推断并约束构造函数与类实例的关系。 Use prototype attributes to infer and constrain the relationship between constructors and class instances.

class BeeKeeper {
    hasMask: boolean;
}

class ZooKeeper {
    nametag: string;
}

class Animal {
    numLegs: number;
}
class Bee extends Animal {
    keeper: BeeKeeper;
}

class Lion extends Animal {
    keeper: ZooKeeper;
}

function findKeeper<A extends Animal, K> (a: {new(): A;
    prototype: {keeper: K}}): K {

    return a.prototype.keeper;
}

findKeeper(Lion).nametag;

by泛型这个很差懂啊,感受也很差用啊

相关文章
相关标签/搜索